0

I noticed if I pass a second parameter to @include like this:

@include('sidebars.pages', array('categories' => Category::all())) 

Then it is possible to replicate the concept of render partials within views and render partials within partials like in Rails.

Do I still need view composers with this functionality?

I appreciate any help!

2 Answers 2

1

Try View Composers to bind data to views. Works best for partial views

// View Composer Example View::composer(array('sidebars.pages'), function($view) { $view->with('categories', Categories::all()); }); @include('sidebars.pages') 
Sign up to request clarification or add additional context in comments.

6 Comments

Hello, where would the view composers live? Could it be in a composers.php file inside the app's root? I don't understand if they have to live in the controller, since they must be available anywhere.
You can create a folder to hold your view composers and include it start/global.php or just have it within in your app/routes.php file.
Ok Mr. mogetutu, I appreciate your answer and comment. Please could you tell me what happens if I need to pass parameters to a class method inside the view composer? Such as Blog_model::recent($post->category->id, 5)
how are you accessing the $post->category->id is it via the url? and are you using L3 or L4?
You can try sessions or caching the variables you are trying to access and accessing them inside the view composer.
|
0

While that may be possible it's not the documented use of @include. I'd use caution when doing it like that, and personally, I wouldn't be calling a model within your view. Bind the data you require from your route or controller. Make use of a presenter to perform any presentation logic to keep your views absolutely clean.

@include injects all currently defined variables into the nested partial view. So if you bound all the categories to the parent view, then used @include('sidebars.pages'), that view would also have the categories bound to it.

1 Comment

Ok, thank you Mr. Lewis, I'll play with presenters and give it a try.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.