【问题标题】:Laravel Blade - Chain/Embed multiple layoutsLaravel Blade - 链/嵌入多种布局
【发布时间】:2023-03-23 15:55:02
【问题描述】:

在我最喜欢的模板框架中,它们通常具有嵌套布局的能力。这在 Blade 中是可能的吗?

例如...

master.blade.php

<html>
  <head><!-- stuff --></head>
  <body>
    @yield('content')
  </body>
</html>

nav.blade.php

@extend('master')
<nav>
    <!-- nav content -->
</nav>
@yeild('content')

面包屑.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)!

  • 有一个一次性的目标网页...参考大师
  • 对于主页...参考导航
  • 对于任何子页面(关于/导航/产品)参考面包屑

布局级联和'content' 将使用已编译的html 在树上向上重建。

这可能吗?我希望避免在布局中使用@include,因为我个人觉得它们很麻烦而且有点眼痛,尤其是当您遇到经常重复但并非无处不在的元素时(面包屑)。

编辑:基于答案。

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

【问题讨论】:

    标签: laravel blade laravel-5.4 laravel-blade


    【解决方案1】:

    我不确定我是否得到你想要的东西。例如,在home.blade.php 中,您扩展了“nav”,而后者又扩展了“master”,但“master”和“nav”都产生了content,因此&lt;home&gt; 内容将呈现两次。

    那么,您的预期输出是什么?我不确定“家”或“关于”是否真的应该extend“导航”或“面包屑”。我认为这两个是一种结构布局组件,所以在主布局中include 对我来说确实有意义。在“导航”中,您可以定义一个部分以在您的视图需要面包屑时进行扩展。

    例如:

    ma​​ster.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
    

    【讨论】:

    • 我在问题中添加了一些内容。这有帮助吗?
    • 另外,链接布局的目的是避免重复任何标记。当布局被链接并且您正在选择内容的注入点时。然后,面包屑就变成了使用面包屑布局并向其传递几个值的问题。绝对没有重复标记...没有条件包含。一般来说,主布局在那个时候有三个......也许四个sectionscontentscriptsstyles,可能还有seo 内容区域。
    【解决方案2】:

    您忘记使用@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>
    

    【讨论】:

      猜你喜欢
      • 2020-06-23
      • 2021-07-06
      • 2017-01-27
      • 2014-03-20
      • 1970-01-01
      • 2014-03-10
      • 2021-07-26
      • 1970-01-01
      • 2018-03-25
      相关资源
      最近更新 更多