Laravel Blade - 链/嵌入多种布局

2024-05-12

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

例如...

master.blade.php

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

导航.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)!

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

布局级联和'content'用编译后的内容重建html当它爬上树时。

这可能吗?我希望避免这样做@include在布局中,我个人觉得它们很麻烦,而且有点让人眼花缭乱,尤其是当你遇到经常重复但不是到处重复的元素(面包屑)时。

编辑:基于答案。

Ideally 'content'将被重建并沿着嵌套布局链向上传递。即如果您有引用的主页nav.blade.php主页内容被添加到导航布局并进行编译。然后由于导航布局引用master.blade.php编译后的布局将被传递到master并再次建造。不复制任何内容。


我不确定我在这里得到你想要的东西。例如在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>

导航.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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Laravel Blade - 链/嵌入多种布局 的相关文章

随机推荐