1

I'm bit stuck at a place. I've got some views of small HTML sections which when combined gives the complete HTML page. I'm trying to build a website builder with Jquery, where I'm having a drop event which adds those particular views:

For example I've got HTML for slideshow:

<div id="slideshow" data-nitsid="2">
    <div class="revolution-slider">
        <ul>
            <!-- SLIDE  -->
            @foreach($contents->slider as $sliders)
                <li data-transition="{{ $sliders->transition }}" data-slotamount="{{ $sliders->slotamount }}" data-masterspeed="{{ $sliders->masterspeed }}">
                    <!-- MAIN IMAGE -->
                    <img src="{{ URL::asset($sliders->url) }}" alt="">
                </li>
            @endforeach
        </ul>
    </div>
</div>

In my JQuery code:

nitsbuilder.dropeventhandler = function ($item, $position) {
    var nits_id = $item.data('nitsid');
    $.ajax({
        method: 'POST',
        url: dropurl,
        data: { nits_id: nits_id, _token: token},
        dataType: 'json',
        success: function (data) {
            nitsbuilder.adder($item, $position, data);
        }
    });

}

Before I was having html codes in the database so it was easier to pull out the html and add to the HTML page, now I'm having html in views, how can I push/include this HTML code or view to ajax request so that my nitsbuilder.adder function executes placing the view through my controller.

My present Controller is:

class DropeventController extends Controller
{
    public function htmlcode(Request $request)
    {
        $pluginid = $request['nits_id'];
        $code = Plugins::findOrFail($pluginid);
        $htmlcode = $code->code;
        return response()->json(['htmlcode' => $htmlcode]);
    }

}

Please guide me. Thanks

6
  • Can you remove the stuff that is already working fine from your code? It should be enough to show your actual ajax call, the routes and the controller, maybe add the migration / table structure you are using here Commented Aug 4, 2016 at 8:28
  • @FrankProvost Done! Commented Aug 4, 2016 at 8:32
  • So currently you store your html snippets in the database? And you would prefer to return blade views? Commented Aug 4, 2016 at 8:33
  • @FrankProvost yes! Commented Aug 4, 2016 at 8:34
  • I'll write an answer - gimme a sec Commented Aug 4, 2016 at 8:37

2 Answers 2

3

You can easily create html strings from blade views using \View::make

e.g. let's assume you have the following folder strucutre

  • project
    • ...
    • ressources
    • views
      • snippets
      • snippetA
      • snippetB

You could now create a route / controller accepting a "name" parameter and then do the following

$name = "snippetA"; // get this from parameters

$html = \View::make("snippets.$name")->render();

You might need to also add variables depending on your views

$name = "snippetA"; // get this from parameters
$errors = []; // error variable might be needed

$html = \View::make("snippets.$name", compact('errors'))->render();

You can then return this html string

return ['html' => $html];

And access it from your ajax done function.

I hope this helps

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for.. Thanks a lot!
2

Suppose your html is in view file called abc.blade.php, you can return the rendered view from your controller in json.

return response()->json([
'htmlcode' => View::make('abc')->render();
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.