如何在 Blade (Laravel 5) 中扩展多个模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35798721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:20:28  来源:igfitidea点击:

How to extend multiple templates in Blade (Laravel 5)?

laravellaravel-5bladelaravel-blade

提问by Yahya Uddin

I have the following files:

我有以下文件:

foo.blade.php

foo.blade.php

<html>
   <head>
   </head>
   <body>
      <h1>Foo Template</h1>
      @yield('content')
   </body>
</html>

bar.blade.php

bar.blade.php

<h2>Bar Template</h2>
<div class="bar-content">
@yield('bar-content')
</div>

I want to create another file that is able to extend both the above templates. e.g something like this:

我想创建另一个能够扩展上述模板的文件。例如这样的事情:

@extends('foo')
@section('content')
     <p>Hello World</p>
     @extends('bar')
     @section('bar-content')
          <p>This is in div.bar-content</p>
     @endsection
@endsection

To give:

给予:

<html>
   <head>
   </head>
   <body>
      <h1>Foo Template</h1>
      <p>Hello World</p>
      <h2>Bar Template</h2>
      <div class="bar-content">
          <p>This is in div.bar-content</p>
      </div>
   </body>
</html>

How can I do this?

我怎样才能做到这一点?

回答by Vishnu M Raveendran

Use multiple files. for eg;

使用多个文件。例如;

layout.blade.php:

layout.blade.php:

@include('header')

@yield('layout_content')

@include('footer')

second.blade.php

第二个.blade.php

@extends('layout')

@section('layout_content')

<div>
@yield('second_content')
</div>

@stop

third.blade.php

第三.blade.php

@extends('second.blade.php')

@section('second_content')

<h1>Hello World!</h1>

@stop

You can include @yield in any of the parent files and can be used in the child

您可以在任何父文件中包含 @yield 并且可以在子文件中使用

回答by Arthur Tarasov

I would recommend using components. Here is an example.

我会推荐使用components。这是一个例子

It seems to me layout/includehas a weird logic when there are many of them and you start nesting them. Components are pretty straight forward. For these nested structures you are building there, components also have slots.

layout/include当它们很多并且你开始嵌套它们时,在我看来有一个奇怪的逻辑。组件非常简单。对于您在那里构建的这些嵌套结构,组件也有插槽。

回答by seschi98

I do not know if this will work, but try @includeinstead of @extendsfor your bartemplate. The put your section for barbelow the other section (not nested). I did not tested this, so I hope it works ;)

我不知道这是否可行,但请尝试@include代替@extends您的bar模板。将您的部分放在bar其他部分下方(未嵌套)。我没有测试过这个,所以我希望它有效;)

// EDIT:

// 编辑:

Try it with an if-statement in your foofile:

尝试在您的foo文件中使用 if 语句:

<html>
    <head>
    </head>
    <body>
    <h1>Foo Template</h1>
    @yield('content')

    @if(isset($displayBar) && $displayBar == true)
        @include('dashboard.test.bar')
    @endif
    </body>
</html>

And now the child view:

现在是子视图:

@extends('dashboard.test.foo')
@section('content')
    <p>Hello World</p>
@endsection

<?php $displayBar = true ?>

@section('bar-content')
    <p>This is in div.bar-content</p>
@endsection

回答by Alexey Mezenin

I think it's not possible to do what you want here, at least without extending Blade: https://laravel.com/docs/5.1/blade#extending-blade

我认为在这里做你想做的事情是不可能的,至少在不扩展 Blade 的情况下是不可能的:https: //laravel.com/docs/5.1/blade#extending-blade

If I were you, I'd rearchitectured my views hierarchy to keep things simple.

如果我是你,我会重新构建我的视图层次结构以保持简单。

回答by faisalsafadi

@php
$ext = '';
@endphp
@if (Request::segment(1)=='explications')
@php
$ext = '_explications'
@endphp
@endif
@extends('pages_templates/template_page_fluid'.$ext)