In 我尝试找到一种通过引用刀片@include来传递变量的方法 https://stackoverflow.com/questions/25437939/how-to-pass-variables-by-reference-to-include-in-a-blade-template,我构建了一个简单的测试用例,它也表明模板的执行顺序非常不稳定。有没有办法在执行顺序很重要的情况下使用带有变量的刀片模板(特别是关于部分)?
测试用例:
testLayout.blade.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
{{"this is the layout: ".++$tabindex."<br>"}}
@include('testInclude')
{{"this is the layout after include: ".++$tabindex."<br>"}}
@include('testInclude',array('tabindex'=>$tabindex))
{{"this is the layout after passing include: ".++$tabindex."<br>"}}
@yield('testYield')
{{"this is the layout after yield: ".++$tabindex."<br>"}}
@section('testSection')
{{"this is the layout section: ".++$tabindex."<br>"}}
@show
{{"this is the layout after section: ".++$tabindex."<br>"}}
</body>
</html>
测试扩展.blade.php
@extends('testLayout')
{{"this is the extension: ".++$tabindex."<br>"}}
@include('testInclude')
{{"this is the extension after include: ".++$tabindex."<br>"}}
@include('testInclude',array('tabindex'=>$tabindex))
{{"this is the extension after passing include: ".++$tabindex."<br>"}}
@section('testYield')
{{"this is the extension yield: ".++$tabindex."<br>"}}
@stop
{{"this is the extension after yield: ".++$tabindex."<br>"}}
@section('testSection')
{{"this is the extension section: ".++$tabindex."<br>"}}
@parent
{{"this is the extension section after parent: ".++$tabindex."<br>"}}
@stop
{{"this is the extension after section: ".++$tabindex."<br>"}}
测试包含.blade.php
{{"this is the include: ".++$tabindex."<br>"}}
路线.php
Route::get('test', function(){
return View::make('testExtension',array('tabindex'=>0));
}); //I have used View::share in stead and the results are identical
Output:
this is the extension: 1
this is the include: 2
this is the extension after include: 2
this is the include: 3
this is the extension after passing include: 3
this is the extension after yield: 5
this is the extension after section: 8
this is the layout: 9
this is the include: 10
this is the layout after include: 10
this is the include: 11
this is the layout after passing include: 11
this is the extension yield: 4
this is the layout after yield: 12
this is the extension section: 6
this is the layout section: 13
this is the extension section after parent: 7
this is the layout after section: 14
分析:
看起来这些值是在扩展中计算的,然后在布局中计算的,然后重新排序。因为这已经是另一个单独的问题,所以请忽略所有 @include 实例都是按值传递的,因此不会影响其包含文件中的值。我通常也不太关心部分之外的值,因为可以理解的是,那里的行为不太可预测。这是有问题的sections.
如果布局对某个值执行任何操作,则将在计算所有扩展值之后计算该值,这是有问题的,因为执行顺序显然不模仿输出顺序。为了让价值观发挥应有的作用,我could@覆盖每个扩展中的所有适用情况,首先就打败了使用模板的点,但是,在这种情况下,我最好在没有任何此类扩展的情况下定义每个视图。
有什么方法可以让这些部分按顺序运行,或者我真的只能对值与顺序无关的内容使用模板吗?