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

断了今生、忘了曾经 提交于 2019-12-10 17:22:44

问题


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


回答1:


Using,

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

Would be the best way.




回答2:


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';



回答3:


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.



来源:https://stackoverflow.com/questions/25437939/how-to-pass-variables-by-reference-to-include-in-a-blade-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!