Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables by reference to @include in a blade template?

In a Laravel 4.2 setup, I have a variable in a template that I wish to share across multiple includes:

master.blade

<?php $tabindex = 0; ?>{{--This is the variable--}}
@include('header'){{-- <-in header.blade, I often use ++$tabindex --}}
{{--$tabindex is still 0--}}
@include('content'){{-- <-in content.blade, I often use ++$tabindex --}}
{{--$tabindex is still 0--}}
@include('footer'){{-- <-in footer.blade, I often use ++$tabindex --}}
{{--$tabindex is still 0--}}

$tabindex if used as a tabindex html attribute is clearly a trivial example that I can get around with safety values and a large enough buffer value, but that's hardly elegant or a solution to the actual problem at hand. In regular php includes, it's my understanding that variable assignment in included files would affect the variables in the including file - this is the desired effect.

I tried View::share(), but it presented the same symptoms. Passing the value to the @include as an array is clearly passing by value and produced the same effect as well.

It almost seems like the including scope values are evaluated first in their entirety, and then the included scopes. If this is the case, it would make what I'm trying to do much less feasible if there is any usage in the including scope or further included scopes (even storing by way of some persisting memory) because the order of execution would be different than the order in the code.

Is there some undocumented blade sorcery to prevent a blade @include from cutting itself off from changing the values of its includer's variables or must I fall back on straight php include or some other ugly alternative (Session variables should persist their values across calling include scopes, but that's just a nasty and flimsy approach)?

like image 705
skovacs1 Avatar asked Aug 22 '14 00:08

skovacs1


1 Answers

Using,

@include('view', array('key'=>'value')) 

Would be the best way.

like image 74
Tim Avatar answered Nov 01 '22 14:11

Tim