How to pass variables via the link to @include in the blade template?

In setting up Laravel 4.2, I have a variable in the template that I want to share a few words with:

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 the tabindex html attribute, is obviously a trivial example of how I can get around safe values ​​and a reasonably large buffer value, but this is hardly elegant or a solution to the real problem. It includes regular php , I understand that assigning a variable in the included files will affect the variables in the included file - this is the desired effect.

I tried View::share() , but it presented the same symptoms. Passing a value to @include as an array explicitly passes by value and gives the same effect.

It seems that all values ​​of the scope are first evaluated in their entirety, and then included by areas. If so, it would do what I am trying to make much less doable if there is any use in the inclusion area or additional included areas (even for storage through some persistent memory), because the order of execution will be different than the order in the code .

Is there an undocumented witchcraft blades so that the @include blade @include not cut itself off from changing the values ​​of its inclusion variables, or should I go back to direct php include or some other ugly alternative ( Session variables should keep their values ​​in all calling areas, but this just a nasty and far-fetched approach)?

+3
source share
3 answers

Using

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

It would be a better way.

+5
source

I take it from what you said that you are doing something like this.

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

And maybe initialize the variable in the template. This practice is not encouraged, but there is another way you can do this, that would be to initialize the variable in the 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 application /start/global.php you add this line.

 require app_path().'/composers.php'; 
0
source

The Laravel blade includes creating a variable area for each template you add.

 View:share('name', $value) 

The difference from what you want is for entering some arbitrary variables into each processed template, it is useful to determine the path to the resources at boot or the entry point of your controller.

To solve your problem, just tell php in the included area to search for the variable above via global , therefore main.blade.php :

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

and in subform.blade.php templates

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

Please note that this may not work if you define the variable not in the main template, I tried it only in the main template (the one that I process in the controller) and it worked.

0
source

Source: https://habr.com/ru/post/1201116/


All Articles