如何弄清楚Laravel刀片@extends的执行顺序? [英] How can I straighten out Laravel blade @extends order of execution?

查看:338
本文介绍了如何弄清楚Laravel刀片@extends的执行顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>我的尝试中通过引用到刀片@include 传递变量的一种方式,我构建了一个简单的测试用例,它还演示了模板的执行顺序非常不稳定. 是否有一种方法可以将刀片模板与变量一起使用,在执行顺序很重要的情况下(特别是对于部分而言)?

In my attempts to find a way to pass a variable by reference to a blade @include, I built a simple test case which also demonstrates that the template order of execution is quite volatile. Is there a way to use blade templates with a variable where the order of execution matters (specifically with regard to sections)?

testLayout.blade.php

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

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

testInclude.blade.php

{{"this is the include: ".++$tabindex."<br>"}}

routes.php

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实例都是按值传递的,因此不会影响其包含文件中的值.通常,我也不太关心部分之外的值,因为可以理解的是,该行为的可预测性较差. 这在部分中是有问题的.

Analysis:

It would appear that the values are calculated in the extension and then calculated in the layout and then re-ordered. Because it's already another separate question, please ignore that all @include instances are pass-by-value and thus do not affect the value in their including files. I'm also generally less concerned about values outside of sections because the behavior there is understandably less predictable. This is problematic in sections.

如果布局对值执行任何操作,则将在计算所有扩展值之后计算该值,这是有问题的,因为执行顺序肯定不会模仿输出顺序.为了使值保持应有的状态,我可以 @覆盖每个扩展中的所有适用情况,首先打败了使用模板的观点,但是在那种情况下,我会完全无需定义任何扩展即可定义每个视图.

If the layout does anything with a value, the value will be calculated after all the extension values are calculated and this is problematic because the order of execution is decidedly not mimicking the output order. To get the values behaving as they should, I could @overwrite all applicable cases in every extension, defeating the point of using templates in the first place, but, in that case, I'd be just as well off to define each view without any such extension at all.

是否有任何方法可以使各节按顺序进行操作,或者我真的可以仅将模板用于与顺序无关的值吗?

推荐答案

模板与顺序无关.可以使用@overwrite来覆盖节,这意味着它们在所有内容加载后都会呈现,否则@overwrite可能会失败.

Templates are order-independent. Sections can be overwritten using @overwrite, which means they are rendered when everything is loaded, otherwise @overwrite could fail.

覆盖部分

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

By default, sections are appended to any previous content that exists in the section. To overwrite a section entirely, you may use the overwrite statement:

@extends('list.item.container')

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


http://laravel.com/docs/templates

这篇关于如何弄清楚Laravel刀片@extends的执行顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆