Laravel Blade - 内部屈服

时间:2016-08-30 16:16:09

标签: laravel

我正试图在另一部分中产生一个部分。但这不能按预期工作,我看到空白输出。

@section('3show')
        This is a text string
@stop

@section('page-content')

<div id="content">
    <article>

        @yield('3show')

    </article>
</div>
<!--#content-->


@stop

任何想要在另一部分中产生部分的想法?

6 个答案:

答案 0 :(得分:4)

好的,这就是我的尝试,我可以确认这是有效的,至少对于Laravel 5+(我有L5.2)。我建议您使用刀片模板。

让我们开始说要将一个部分放到另一个部分,你必须在容器部分定义之前定义你所包含的部分。所以,有了这个,我解决了这种情况:

我有一个主刀片(main.blade.php)模板,其中包含:

<section class="content">
 <!-- Your Page Content Here -->
 @yield('main-content')
</section><!-- /.content -->

我有第二个刀片(common.blade.php)模板,其中包含您可能希望在多个页面中显示的常见内容以及定义主内容部分的内容。这个看起来像:

@section('main-content')
  <div class="container">
    @yield('extra-content')
  </div>
@endsection

最后我得到了第3个模板(test.blade.php),它扩展了主模板并包含了我想要展示的常见内容,但要小心,因为顺序很重要。这个看起来像:

@extends('main')
@section('extra-content')
  <div>
    <span> This is a test! </span>
  </div>
@endsection
@include('common')

在您的控制器或路线中(无论您何时返回视图),都应返回第3个模板。

答案 1 :(得分:2)

在我的项目中,我创建了一些部分以便拥有更清晰的代码,我给它们作为一个例子:3show.blade.php。为了在一个部分中使用它们,我只需要包含它们。 我想这会做你想要的。

{{1}}

答案 2 :(得分:1)

解决方案1: 您可以在部分末尾使用@show代替@stop 然后laravel会渲染你的@yield部分...

像这样:

@section('page-content')

<div id="content">
    <article>

              @yield('3show')

    </article>
</div>  

@show  # not @stop




@section('3show')

        This is a text string
@stop

这种方式可以让您查看并显示结果 因此,如果您将您的部分分两次,那么结果将被翻两次

解决方案2:

在yiel之前插入通话部分 像这样:

 **first call section :**

@section('3show')

        This is a text string
@stop

**then define your section : 3show**


@section('page-content')

<div id="content">
    <article>

              @yield('3show')

    </article>
</div>  

@stop  **not @show**

答案 3 :(得分:0)

假设您有两个文件:

-article_base.blade.php - &gt;每篇文章中的默认数据。

-article_index.blade.php - &gt;自定义文件。

article_base.blade.php
@extends('layout')
@section('page-content')

<div id="content">
    <article>
        ....
        @yield('3show')
        ....
    </article>
</div>
<!--#content-->
@stop

article_index.blade.php

@extends('article_base')
@section('3show')
   This is a text string
@endsection

我希望这有效

答案 4 :(得分:0)

您必须在主模板中同时使用@include和@yield子模板才能在主模板内的特定位置添加子模板(不仅仅是在主模板之前或之后 - 这是通过添加子模板中的@parent - 但介于两者之间):

main.blade.php

@include('child')
<div>
     ...
     @yield('child')
</div>

child.blade.php

@section('child')
     <div>
          ...
     </div>
@endsection

答案 5 :(得分:0)

我有同样的问题。

在这种情况下,您不能使用@extends选项,而需要使用@include。

所以说你有:

  1. 根目录布局(layouts / app.blade.php)
  2. 额外的布局(layouts / extra.blade.php)
  3. 您正在调用的实际视图(someview.blade.php)

解决方案是使用添加/继承根布局(1)到视图的顶行(3):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.1.0</version>
    <dependencies>
        <dependency>
            <groupId>com.puppycrawl.tools</groupId>
            <artifactId>checkstyle</artifactId>
            <version>...choose your version...</version>
        </dependency>
    </dependencies>
</plugin>

接下来,在视图的第二行添加/继承额外的布局(2),但使用@include而不是@extends:

@extends('layouts.app')

...,请记住将额外布局的内容包装在@section名称中,例如 @section('extra')

最后,您可以从视图中调用额外的布局内容:

@include('layouts.extra')

因此,总而言之,您的someview.blade.php视图将如下所示:

  <p>Look below, you will see my extra content...</p>
  @yield('extra')
相关问题