【问题标题】:How can I straighten out Laravel blade @extends order of execution?如何理顺 Laravel Blade @extends 的执行顺序?
【发布时间】: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 在每个扩展中的所有适用情况,首先破坏了使用模板的意义,但是,在这种情况下,我会像完全不用任何扩展来定义每个视图。

有什么方法可以让这些部分按顺序运行,或者我真的只能将模板用于值与顺序无关的事情吗?

【问题讨论】:

    标签: php laravel-4 blade


    【解决方案1】:

    模板与顺序无关。可以使用@overwrite 覆盖部分,这意味着它们会在加载所有内容时呈现,否则@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

    【讨论】:

    • +1 但是...您能否格式化您的答案,例如 ***(水平线)而不是 ==== 并使用代码块突出显示代码。谢谢!
    • 感谢您的努力(并指出一个错字(@override 应该是 @overwrite))。这不能回答是/否的问题。我的测试表明模板与顺序无关。我在分析中说过,我可以在每个扩展中使用 @overwrite 来获取我需要的有序值(通过重新定义整个页面来克服模板化的问题)。如果您可以明确确认无法在保留订单的情况下进行模板化,您能否将其添加到答案中?对发生这种情况的来源的引用就足以证明这一点,并且可以作为解决此问题的开始。
    • 另外,我强烈怀疑这实际上是 Symphony 的一个问题,据我所知,这个模板引擎源于此。
    猜你喜欢
    • 2016-12-18
    • 2017-07-12
    • 2017-11-24
    • 2015-09-12
    • 2021-07-06
    • 2020-01-27
    • 2015-10-09
    • 2020-11-12
    • 2021-06-22
    相关资源
    最近更新 更多