【发布时间】:2014-10-19 05:29:48
【问题描述】:
在my attempts to find a way to pass a variable by reference to a blade @include 中,我构建了一个简单的测试用例,它也证明了模板的执行顺序非常不稳定。 有没有办法在执行顺序很重要(特别是关于部分)的情况下使用带有变量的刀片模板?
测试用例:
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>
testExtension.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>"}}
testInclude.blade.php
{{"this is the include: ".++$tabindex."<br>"}}
routes.php
Route::get('test', function(){
return View::make('testExtension',array('tabindex'=>0));
}); //I have used View::share in stead and the results are identical
输出:
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 实例都是按值传递的,因此不会影响其包含文件中的值。我通常也不太关心部分之外的值,因为可以理解的是那里的行为难以预测。 这在部分中存在问题。
如果布局对值进行任何操作,则将在计算所有扩展值之后计算该值,这是有问题的,因为执行顺序显然不是模仿输出顺序。为了使值按应有的方式运行,我可以 @overwrite 在每个扩展中的所有适用情况,首先破坏了使用模板的意义,但是,在这种情况下,我会像完全不用任何扩展来定义每个视图。
有什么方法可以让这些部分按顺序运行,或者我真的只能将模板用于值与顺序无关的事情吗?
【问题讨论】: