Laravel Blade - 链/嵌入多种布局

Jar*_*red 13 laravel blade laravel-blade laravel-5.4

在我最喜欢的模板框架中,它们通常具有嵌套布局的能力.Blade中有可能出现这种情况吗?

例如...

master.blade.php

<html>
  <head><!-- stuff --></head>
  <body>
    @yield('content')
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

nav.blade.php

@extend('master')
<nav>
    <!-- nav content -->
</nav>
@yeild('content')
Run Code Online (Sandbox Code Playgroud)

breadcrumb.blade.php

@extend('nav')
<breadcrumb>
    <!-- breadcrumb content -->
</breadcrumb>
@yield('content')
Run Code Online (Sandbox Code Playgroud)

home.blade.php

@extend('nav')
@section('content')
    <home>
        <!-- content -->
    </home>
@endsection
Run Code Online (Sandbox Code Playgroud)

about.blade.php

@extend('breadcrumb')
@section('content')
    <about>
        <!-- content -->
    </about>
@endsection
Run Code Online (Sandbox Code Playgroud)

我喜欢这种格式的原因是它能够选择你的注射点使它非常优雅(IMO)!

  • 有一个登陆页面...参考大师
  • 对于主页...参考导航
  • 对于任何子页面(关于/ nav/product)引用breadcrumb

布局级联并'content'在编译时html随着树一样重建.

这可能吗?我希望避免@include在布局中做,因为我个人发现它们很麻烦,有点眼睛酸痛,特别是当你得到经常重复的元素,但不是到处都是(面包屑).

编辑:根据答案.

理想情况下,'content'将重建并传递嵌套布局链.即如果您有引用nav.blade.php主页内容的主页,则会将其添加到导航布局并进行编译.然后,由于导航布局引用master.blade.php已编译的布局将被传递到master并再次构建.没有重复任何内容.

ale*_*ino 6

我不确定我能得到你在这里的东西.例如,在home.blade.php扩展"nav"时,它会扩展"master",但"master"和"nav"都会产生content,因此<home>内容将呈现两次.

那么,你的预期产量是多少?我不确定"家"或"约"应该真正extend"导航"或"面包屑".我认为这两种结构布局组件,所以它include在主布局中对我们有意义.在"nav"中,您可以定义一个部分,以便在您的视图需要面包屑时进行扩展.

例如:

master.blade.php

<html>
<head><!-- stuff --></head>
  <body>
    @include('nav')        
    @yield('content')
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

nav.blade.php

<nav>
  <!-- nav content -->
  @yield('breadcrumb')
</nav>
Run Code Online (Sandbox Code Playgroud)

home.blade.php

@extend('master')

@section('content')
  <home>
    <!-- content -->
  </home>
@endsection
Run Code Online (Sandbox Code Playgroud)

about.blade.php

@extend('master')

@section('breadcrumb')
  <breadcrumb>
    <!-- breadcrumb content -->
  </breadcrumb>
@endsection

@section('content')
  <about>
    <!-- content -->
  </about>
@endsection
Run Code Online (Sandbox Code Playgroud)


kri*_*lfa 3

你忘了使用@parent. 这是例子:


master.blade.php

<html>
  <head>
    {{-- Stuff --}}
  </head>
  <body>
    @yield('content')
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

nav.blade.php

你需要把它放在nav里面section告诉master布局这是一个内容。如果不这样做,nav将位于布局的顶部master(是的,在 html 之外)。

@extends('master')

@section('content')
  <nav>
    <p>nav content</p>
  </nav>
@endsection
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

渲染:

home.blade.php

<html>
<head>
</head>
<body>
  <nav>
    <p>nav content</p>
  </nav>
  <home>
    <p>home content</p>
  </home>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)