【问题标题】:Nesting sections in BladeBlade 中的嵌套部分
【发布时间】:2018-07-03 15:38:15
【问题描述】:

我正在研究 Blade 如何实现 @section,但它的工作方式与我习惯使用 Twig 时有所不同。看来你不能互相嵌套@sections。

例子:

_layout.blade.php(基本布局文件)

<html>

    //favicons
    //meta content
    //other basic shizzle

    <head>
        <title>overal</title>
    </head>

    <body>
        @yield('body_content')
    </body>

</html>

website.blade.php(具体实现)

@extend('_layout.blade.php')

@section('body_content')

    <div class="">
        website-header
    </div>

    <div class="main">

        @section('navigation')
            <nav>website navigation</nav>
        @endsection

        <div class="website">

            {-- the actual content --}
            @yield('content')

        </div>

    </div>

@endsection

blog.blade.php(博客页面的实现)

@extend('website.blade.php')

@section('content')
    blog-items
@endsection

faq.blade.php(常见问题页面的实现)

@extend('website.blade.php')

{{-- we don't want the navigation here or want something different --}}
@section('nav')@endsection

@section('content')
    faq-items
@endsection

我无法使用@includes 解决它,因为我希望能够灵活地确定子视图中的子子部分并覆盖它们的内容。

【问题讨论】:

  • 您可以使用@extend('website.blade.php', ['nav' =&gt; false]) 将变量从视图传递到视图,然后您可以在website.blade.php 中查询此变量并删除导航(对于此特定实例,这可能会给您一些地方延伸自)
  • 好吧,我不想把我的基本模板(或者它可能有多深)与变量混在一起,以防孩子需要覆盖/其他东西。但有可能......所以我的问题不再相关。我确实在下面发布了这个,以防有人可能会寻找相同的东西:)

标签: laravel blade laravel-blade


【解决方案1】:

没关系,这可能的 困扰我的是我的编辑器(PhpStorm)。你可以将@section相互嵌套并在扩展模板中覆盖/重新定义它们^^

W00000吨

【讨论】:

猜你喜欢
  • 2021-04-19
  • 2015-06-11
  • 2018-03-07
  • 2015-03-11
  • 1970-01-01
  • 2013-06-01
  • 2015-04-30
  • 2016-10-02
  • 2017-08-31
相关资源
最近更新 更多