you can include blade partials into other views via @include('folder.filename')
then you need a view composer that listens to folder.filename
and injects the necessary variable into that view
source: http://laravel.com/docs/templates (including sub views)
Use a single view as template for all views or extends BaseController, and use
View::share($arrayOfDataAvailableToAllViews);
es.
class BaseController2 extends BaseController{
public function __construct(){
$data = array('foo'=>'Foo', 'bar'=>'Bar');
View::share($data);
}
}
Then if your controllers extends BaseController2, all views will share $data array.
If $data will be a db query result cache it
$data = MyModel::remember(10)->get();
longilineo said:
class BaseController2 extends BaseController{ $data = array('foo'=>'Foo', 'bar'=>'Bar'); View::share($data); }
Mmmh, that View::share... shouldn't it be inside a function? I get the following error:
syntax error, unexpected '$data' (T_VARIABLE), expecting function (T_FUNCTION)
yes, there was an error, take a look at updated reply
Then I don't get the error but the variable is not recognized:
Undefined variable: data (View: /Users/Maggie/Sites/hotesses/laravel/app/views/master.blade.php)
In your views you should have $foo and $bar variables available.
longilineo said:
In your views you should have $foo and $bar variables available.
Duh, of course, my bad, thanks a lot
Ok, it works with the array, but how does it work with the db query? I have:
$hostesses = Hostess::remember(10)->get();
View::share($hostesses);
And upon using var_dump($hostesses) I get an Illegal offset type error
Have you checked out view composers ?
http://laravel.com/docs/responses#view-composers
You can make it work for multiple views. Possibly all.
I get an Illegal offset type error
Where? In views? Or in BaseController2?
In order to retrieve $hostesses variable in views, in BaseController2 you should have:
$hostesses = Hostess::remember(10)->get();
View::share(array("hostesses"=>$hostesses));
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community