如何理顺Laravel刀片@extends执行顺序?

时间:2014-08-26 01:02:28

标签: php laravel-4 blade

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每个扩展中的所有适用情况,首先打败使用模板的点,但是,在这种情况下,我会成为同样可以定义每个视图而不需要任何这样的扩展。

有没有办法让这些部分按顺序运行,或者我真的只能将模板用于值与顺序无关的事物吗?

1 个答案:

答案 0 :(得分:1)

模板与订单无关。可以使用@overwrite覆盖节,这意味着它们在加载所有内容时呈现,否则@overwrite可能会失败。


覆盖章节

默认情况下,部分会附加到该部分中存在的任何先前内容中。要完全覆盖某个部分,您可以使用覆盖语句:

@extends('list.item.container')

@section('list.item.content')
    <p>This is an item of type {{ $item->type }}</p>
@overwrite

http://laravel.com/docs/templates