I want to offload some HTML sections of my site into partials (includes) files rather than repeating the information in each view or layout. However, I also need them to contain specific data. An obvious example is the head section; I would like to put this into a partial, however I also want to be able to set the document title and META fields for each page.
I think the best way is to use Laravel's view-composer
As per the docs, I can use an array to define the composer for multiple views at once:
View::composer(array('view1','view2', 'view3'), function($view)
{
$view->with('count', User::count());
});
However, what if I want to use this composer for every view, as I do in this case?
There's a few answers kicking around SO (such as this one) which suggests I use a wildcard. So here's my code:
View::composer('*', function($view)
{
$view->with('header', View::make('partials.head', $view->getData()));
$view->with('footer', View::make('partials.footer', $view->getData()));
});
Here's the problem: Using this is currently giving me an out of memory error, which suggests that it is very inefficient (and therefore that I really shouldn't be doing this).
So do I really need to pass an array listing every page on my website?
Isn't there a way to use composer for every page rendered, like I can with View::share();
?