php - Laravel Blade : @extend a (already extended) child blade?

标签 php laravel laravel-blade

有人知道是否可以延长 child Blade 吗?

我的应用程序有一个通用的布局模板,然后每个页面都从该模板@extends。 每个页面都可以根据需要为其他 HTML block (例如模态)引入一系列 @includes。

@extends('common.layout')

@section('content')
    ... Some other content ...
    @include('modals.modal-1')
    @include('modals.modal-2')
@endsection

所有的模态都有很多通用的样板代码(Bootstrap),所以我想做的是定义一个主模型模板,让所有的模态从它@extend,然后在我的页面中@include那些作为必需的。所以/modals/modal-1.blade.php 看起来像这样:

@extends('common.modals')

@section('modal-id', 'modal-1')
@section('modal-title','Modal title')

@section('modal-body')
    ... Some HTML / Blade here ...
@endsection

但每次尝试时,生成的 HTML 都会损坏。在上述情况下,modal-1 会显示两次,因为它首先出现,而 modal-2 根本不会出现。颠倒顺序,modal-2 将出现两次。

我想我可以简单地将模态主体放在一个变量中并在@include 语句中使用它 @include('modal.blade', ['body' => '<div>...</div>']) , 但使用 @extends 感觉更正确。

有什么想法吗?

最佳答案

您完全可以从已经扩展的 View 扩展 Blade View 。但是,您将模板继承与 View 包含混合在一起,这会导致您出现奇怪的结果。

当使用 @extend 指令继承模板时,您必须始终返回您想要的链上最低的子级。假设您有 3 代模板:

//grandparent.blade.php
<html>
<head>
    <title>My App</title>
</head>
<body>
    @yield('parent-content')
</body>
</html>

//parent.blade.php
@extends('grandparent')

@section('parent-content')
<div class="container">
    @yield('child-content')
</div>
@endsection

//child.blade.php
@extends('parent')

@section('child-content')
<div class="page">
    //stuff
</div>
@endsection

在这种情况下,您将返回 subview ,它还将包含其上方的两代模板。但是您可以永远返回parent.blade.php 并期望它也返回那个 subview 。可能有 100 个扩展父 View 的 subview ,因此无法知道是哪一个。

@include 指令视为一种将 View 中的 HTML 很好地分解成更小部分的方法。通常,您会将它用于要在多个 View 中引用的可重用代码片段。不过,它与模板继承不同。还要记住,包含的 View 将获得与其父 View 相同的所有数据(您甚至可以传递更多数据)。

在您的情况下,您必须决定什么构成页面的基本根。页面的核心是modal-1吗?如果是这样,您需要从您的 Controller 返回 modal-1 作为您的 subview 并将其向上扩展。在这种情况下,请将文件完全保留在您的帖子中。它的父 View (common.modals) 需要更改为:

@extends('common.layout')

@section('content')
    ... Some other content ...

    @yield('modal-id')
    @yield('modals-title')
    @yield('modal-body')

    @include('modals.modal-2')
@endsection

显然,您会将每个 yield 语句放在页面上有意义的地方。

但是,如果 modal-1 不是页面的核心,而只是您想要包含的额外内容(如小部件),那么您应该 include 它就像您在父 View 中所做的那样。在这种情况下,您将需要从中删除 @extends 指令,并且不必费心将主要 HTML 包装在任何部分中。它将按原样传递给 View 。如果您在包含的模板中包含部分,则必须在包含它的 View 中生成这些部分。所以你的 modal-1 模板最终会看起来像这样:

<div>
    <p>HTML goes here. No need to extend anything or wrap in a section.
       You can still include {{$data}} though.
    </p>
</div>

@section('script')
    Including this section means that the view that is including this template
    already contains a @yield('script') directive.
    By including the @parent directive, this section will append that one.

@parent
@endsection

关于php - Laravel Blade : @extend a (already extended) child blade?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33344580/

相关文章:

php - .htaccess php_value display_errors 设置因错误 500 而中断

laravel - 调用未定义的方法 Illuminate\Foundation\Application::registerCoreContainerAliases()

php - 错误:调用未定义的方法Tests\Unit\SomeTest::assertStatus()

javascript - 如何在 Laravel Blade 中的 React 组件中获取 Laravel 验证错误

php - laravel 如何路由到 javascript 上的路由?

php - 如何在php的for循环中显示图像?

PHP 错误 : "Cannot pass parameter 2 by reference"

PHP5 : How come an included function always echoes first if I call it?

php - Blade foreach 将输出转换到对象

php - 在 Laravel Blade 文件中使用 css 样式标签