gpt4 book ai didi

php - 我怎样才能理顺 Laravel blade @extends 的执行顺序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:47:38 25 4
gpt4 key购买 nike

my attempts to find a way to pass a variable by reference to a blade @include ,我构建了一个简单的测试用例,它也证明了模板的执行顺序非常不稳定。 有没有办法在执行顺序很重要(特别是关于部分)的情况下使用带有变量的 Blade 模板?

测试用例:

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>"}}

testInclude.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

输出:

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所有适用的情况,首先打败了使用模板的意义,但是,在那种情况下,我会像在没有任何此类扩展的情况下定义每个 View 会很好。

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

最佳答案

模板与顺序无关。可以使用 @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

关于php - 我怎样才能理顺 Laravel blade @extends 的执行顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25496506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com