Laravel blade 个包含视图中的部分(blade 概念)

Laravel blade sections inside an included view (bladeception)

我的 blade 结构有问题。我有一个基本布局模板、一个内容页面模板和子模板。

将以下片段作为参考

基本模板

<!DOCTYPE html>
<html lang="en">
    @include('head')
</head>

<body>
@include('body-open')
@yield('main')
@include('footer')
@include('body-close')

</body>
</html>

内容模板

@extends('base')
@section('main')
    content

@include('my-other-section-that-has-another-section-declaration-inside')

@overwrite

@section('body.script')
this is an extended script
@stop

body-close模板

@section('body-open')
    this is the original content
@stop

my-other-section-that-has-another-section-declaration-inside模板

this is cool

@section('body-open')
    @parent
    this should append to body open.
@stop

这是我的预期结果

<!DOCTYPE html>
<html lang="en">

</head>

<body>
this is the original content
this should append to body open.

content

this is cool
</body>
</html>

但是我得到的是这个内容

<!DOCTYPE html>
<html lang="en">

</head>

<body>
this is the original content

content
this is cool

</body>
</html>

请注意,行 this 应该附加到 body open。 已被跳过,不会附加到其预期的部分。

我的代码有问题吗?或者这种方法可行吗?

谢谢!

检查您的包含顺序。

您正在加载此部分内容:my-other-section-that-has-another-section-declaration-inside template 首先:

@section('body-open')
    @parent
    this should append to body open.
@stop

在上面你试图调用 @parent - 但是这个是空的所以你在 body-open 部分只有这个:this should append to body open. 目前。

之后:@include('body-close')你将得到:

@section('body-open')
    this is the original content
@stop

...没有 @parent 调用 - 因此您将覆盖此部分: this is the original content - 这就是您在这里所能期待的。您可以尝试这样修复它:

@section('body-open')
    this is the original content
    @parent
@stop