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