Sometimes one may want to load a View with some PHP data passed to it and store it in a variable inside the Controller’s action. This rendered view data might be later passed to another view which is finally loaded and dumped to the user’s screen.
Doing so in Laravel is pretty simple, let me show you how:
What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.
$view = View::make('my_view', ['name' => 'Rishabh']); $contents = (string) $view; // or $contents = $view->render();
We basically make a view that is to be returned to the browser and store the object in a variable called $view
. Then we convert that object to its string representation which is basically the contents of the view (with all the dynamic PHP data injected) or call the render
method on the object to return the same content.
Hope that helps!
Thanks man.. made my day.
Thank god i found this post. I was going crazy how to do this.
I was trying to pass the html inside a JSON field as i need to pass much more variables in that same request.
Thank you very much.
Thanks for the tutorial. Would this not do thesame:
$contents = View::make(‘my_view’, [‘name’ => ‘Rishabh’])->render();
Yes it would, I just didn’t chain since there were two different ways to be shown.
Thanks Bro! I use the view render with str_replace so that I can use tag like %form% and output the form! Thanks again for the trick!
It’s Works! Thanks You >\\\<
you’re my hero!!