Laravel Blade - Chain/Embed 多种布局
Laravel Blade - Chain/Embed multiple layouts
在我最喜欢的模板框架中,它们通常具有嵌套布局的能力。这在 Blade 中是可能的吗?
例如...
master.blade.php
<html>
<head><!-- stuff --></head>
<body>
@yield('content')
</body>
</html>
nav.blade.php
@extend('master')
<nav>
<!-- nav content -->
</nav>
@yeild('content')
breadcrumb.blade.php
@extend('nav')
<breadcrumb>
<!-- breadcrumb content -->
</breadcrumb>
@yield('content')
home.blade.php
@extend('nav')
@section('content')
<home>
<!-- content -->
</home>
@endsection
about.blade.php
@extend('breadcrumb')
@section('content')
<about>
<!-- content -->
</about>
@endsection
我喜欢这种格式的原因是它让选择注入点变得非常优雅 (IMO)!
- 有一个一次性登陆页面...参考大师
- 首页...参考导航
- 对于任何子页面(about/nav/product)参考面包屑
布局级联,并且 'content'
在编译后的 html
上重建。
这可能吗?我希望避免在布局中做 @include
,因为我个人觉得它们很麻烦并且有点眼痛,尤其是当您遇到经常重复但并非到处都重复的元素(面包屑)时。
编辑:基于答案。
理想情况下 'content'
将被重建并向上传递嵌套布局链。即,如果您有引用 nav.blade.php
的主页,则主页内容将添加到导航布局并进行编译。然后,由于导航布局引用 master.blade.php
,编译后的布局将被传递到 master
并再次构建。禁止复制任何内容。
我不确定我是否明白您的意思。例如,在 home.blade.php
中,您扩展了 "nav",它又扩展了 "master",但是 "master" 和 "nav" 都产生了 content
,所以 <home>
内容将呈现两次。
那么,您的预期输出是多少?我不确定 "home" 或 "about" 是否真的应该 extend
"nav" 或 "breadcrumb"。我将这两个视为某种结构布局组件,因此在主布局中 include
它们对我来说确实有意义。在 "nav" 中,您可以定义一个部分,以便在您的视图需要面包屑时进行扩展。
例如:
master.blade.php
<html>
<head><!-- stuff --></head>
<body>
@include('nav')
@yield('content')
</body>
</html>
nav.blade.php
<nav>
<!-- nav content -->
@yield('breadcrumb')
</nav>
home.blade.php
@extend('master')
@section('content')
<home>
<!-- content -->
</home>
@endsection
about.blade.php
@extend('master')
@section('breadcrumb')
<breadcrumb>
<!-- breadcrumb content -->
</breadcrumb>
@endsection
@section('content')
<about>
<!-- content -->
</about>
@endsection
您忘记使用 @parent
。这是示例:
master.blade.php
<html>
<head>
{{-- Stuff --}}
</head>
<body>
@yield('content')
</body>
</html>
nav.blade.php
你需要把 nav
放在 section
里面来告诉 master
布局这是一个内容。如果不这样做,nav
将位于 master
布局的顶部(是的,在 html 之外)。
@extends('master')
@section('content')
<nav>
<p>nav content</p>
</nav>
@endsection
home.blade.php
在此示例中,content
部分使用 @parent
指令将内容附加(而不是覆盖)到布局的侧边栏。 @parent
指令将在呈现视图时被布局的内容替换。
@extends('nav')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<home>
<p>home content</p>
</home>
{{-- You can put your @parent here, give it a try --}}
@endsection
breadcrumb.blade.php
@extends('nav')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<breadcrumb>
<p>breadcrumb content</p>
</breadcrumb>
{{-- You can put your @parent here, give it a try --}}
@endsection
about.blade.php
@extends('breadcrumb')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<about>
<p>about content</p>
</about>
{{-- You can put your @parent here, give it a try --}}
@endsection
渲染:
home.blade.php
<html>
<head>
</head>
<body>
<nav>
<p>nav content</p>
</nav>
<home>
<p>home content</p>
</home>
</body>
</html>
about.blade.php
<html>
<head>
</head>
<body>
<nav>
<p>nav content</p>
</nav>
<breadcrumb>
<p>breadcrumb content</p>
</breadcrumb>
<about>
<p>about content</p>
</about>
</body>
</html>
在我最喜欢的模板框架中,它们通常具有嵌套布局的能力。这在 Blade 中是可能的吗?
例如...
master.blade.php
<html>
<head><!-- stuff --></head>
<body>
@yield('content')
</body>
</html>
nav.blade.php
@extend('master')
<nav>
<!-- nav content -->
</nav>
@yeild('content')
breadcrumb.blade.php
@extend('nav')
<breadcrumb>
<!-- breadcrumb content -->
</breadcrumb>
@yield('content')
home.blade.php
@extend('nav')
@section('content')
<home>
<!-- content -->
</home>
@endsection
about.blade.php
@extend('breadcrumb')
@section('content')
<about>
<!-- content -->
</about>
@endsection
我喜欢这种格式的原因是它让选择注入点变得非常优雅 (IMO)!
- 有一个一次性登陆页面...参考大师
- 首页...参考导航
- 对于任何子页面(about/nav/product)参考面包屑
布局级联,并且 'content'
在编译后的 html
上重建。
这可能吗?我希望避免在布局中做 @include
,因为我个人觉得它们很麻烦并且有点眼痛,尤其是当您遇到经常重复但并非到处都重复的元素(面包屑)时。
编辑:基于答案。
理想情况下 'content'
将被重建并向上传递嵌套布局链。即,如果您有引用 nav.blade.php
的主页,则主页内容将添加到导航布局并进行编译。然后,由于导航布局引用 master.blade.php
,编译后的布局将被传递到 master
并再次构建。禁止复制任何内容。
我不确定我是否明白您的意思。例如,在 home.blade.php
中,您扩展了 "nav",它又扩展了 "master",但是 "master" 和 "nav" 都产生了 content
,所以 <home>
内容将呈现两次。
那么,您的预期输出是多少?我不确定 "home" 或 "about" 是否真的应该 extend
"nav" 或 "breadcrumb"。我将这两个视为某种结构布局组件,因此在主布局中 include
它们对我来说确实有意义。在 "nav" 中,您可以定义一个部分,以便在您的视图需要面包屑时进行扩展。
例如:
master.blade.php
<html>
<head><!-- stuff --></head>
<body>
@include('nav')
@yield('content')
</body>
</html>
nav.blade.php
<nav>
<!-- nav content -->
@yield('breadcrumb')
</nav>
home.blade.php
@extend('master')
@section('content')
<home>
<!-- content -->
</home>
@endsection
about.blade.php
@extend('master')
@section('breadcrumb')
<breadcrumb>
<!-- breadcrumb content -->
</breadcrumb>
@endsection
@section('content')
<about>
<!-- content -->
</about>
@endsection
您忘记使用 @parent
。这是示例:
master.blade.php
<html>
<head>
{{-- Stuff --}}
</head>
<body>
@yield('content')
</body>
</html>
nav.blade.php
你需要把 nav
放在 section
里面来告诉 master
布局这是一个内容。如果不这样做,nav
将位于 master
布局的顶部(是的,在 html 之外)。
@extends('master')
@section('content')
<nav>
<p>nav content</p>
</nav>
@endsection
home.blade.php
在此示例中,content
部分使用 @parent
指令将内容附加(而不是覆盖)到布局的侧边栏。 @parent
指令将在呈现视图时被布局的内容替换。
@extends('nav')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<home>
<p>home content</p>
</home>
{{-- You can put your @parent here, give it a try --}}
@endsection
breadcrumb.blade.php
@extends('nav')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<breadcrumb>
<p>breadcrumb content</p>
</breadcrumb>
{{-- You can put your @parent here, give it a try --}}
@endsection
about.blade.php
@extends('breadcrumb')
@section('content')
{{-- You can control where @parent should be rendered --}}
@parent
<about>
<p>about content</p>
</about>
{{-- You can put your @parent here, give it a try --}}
@endsection
渲染:
home.blade.php
<html>
<head>
</head>
<body>
<nav>
<p>nav content</p>
</nav>
<home>
<p>home content</p>
</home>
</body>
</html>
about.blade.php
<html>
<head>
</head>
<body>
<nav>
<p>nav content</p>
</nav>
<breadcrumb>
<p>breadcrumb content</p>
</breadcrumb>
<about>
<p>about content</p>
</about>
</body>
</html>