5

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)?

skovacs1
  • 461
  • 1
  • 6
  • 14

3 Answers3

7

Using,

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

Would be the best way.

Sam
  • 7,252
  • 16
  • 46
  • 65
Tim
  • 380
  • 2
  • 8
  • Like I said in the question "Passing the value to the @include as an array is clearly passing by value and produced the same effect as well." While I thought that might possibly work too way back when I tried it before, it is still pass by value when I try it Today (which is possibly obvious from the way it is called). This answer is very short - maybe there's some crucial detail of your solution that you neglected to share in order to make this pass by reference? – skovacs1 Aug 25 '14 at 16:43
0

I take it from what you said, that you've been doing something like this.

View::share('some_variable',$some_variable);

And maybe initialized the variable in the template. This practice is discouraged, but there-s another way you can do it, which would be to initialize the variable in a php file and share it from there by adding this line to the file.

$some_variable = 0;  //Initialize it any way you need to.
View::share('some_variable', $some_variable);

And then in your app/start/global.php you add this line.

require app_path().'/composers.php';
arrigonfr
  • 742
  • 3
  • 12
  • 34
  • I have tried exactly as you suggest with the simple example above, but the problem persists - I declared the variable in a file and perform View::share immediately after. I then added the require for that file to the global start and removed all direct assignment in the layout such that the only assignment would be the pre-incrementation in the included blades. The tabindex is still back to its initial value at the beginning of each include. Did I miss something in the process here? – skovacs1 Aug 22 '14 at 16:32
0

Laravel blade include seem to create a variable scope for every included template you add.

View:share('name', $value)

Does different thing from what you want, it is intended to inject some arbitrary variables to every template rendered, it's usefull to define assets path in bootstrap or entry point of your controller.

To solve your problem, just tell php in the included scope to look up for a variable above via global, so main.blade.php:

<?php $tabIndex = 0 ?>
@include('subform');

and in templates subform.blade.php

<?php 
    global $tabindex;
    $tabindex++;
?>

Note, this might not work if you define the variable not in a main template, I have tried this only at main template (the one that I render to in controller) and it worked.

Brock
  • 1,635
  • 2
  • 18
  • 27