Laravel Blade - 屈服内部部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39232476/
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
Laravel Blade - yield inside section
提问by Johny Pie
I'm trying to yield a section inside another section. But this does not work as expected, I see blank output.
我试图在另一个部分中产生一个部分。但这并没有按预期工作,我看到空白输出。
@section('3show')
This is a text string
@stop
@section('page-content')
<div id="content">
<article>
@yield('3show')
</article>
</div>
<!--#content-->
@stop
Any ideas to yield section inside another section ?
在另一个部分中产生部分的任何想法?
采纳答案by Julian Rodriguez
Ok, this is what I tried and I can confirm that this works, at least for Laravel 5+ (I have L5.2). This is how I suggest you to use your blade templates.
好的,这就是我尝试过的,我可以确认这有效,至少对于 Laravel 5+(我有 L5.2)。这就是我建议您使用刀片模板的方式。
Lets start saying that to yield a section into another section you have to define your included section before container section definition. So, with that clear, I solved this situation like this:
让我们开始说,要将一个部分生成到另一个部分中,您必须在容器部分定义之前定义包含的部分。所以,明确地说,我解决了这样的情况:
I got a main blade (main.blade.php) template which has something like:
我有一个主刀片 (main.blade.php) 模板,它有类似的东西:
<section class="content">
<!-- Your Page Content Here -->
@yield('main-content')
</section><!-- /.content -->
I got a second blade (common.blade.php) template which has that common stuff you may want to show across many pages and where main-content section is defined. This one looks like:
我得到了第二个刀片 (common.blade.php) 模板,其中包含您可能希望在多个页面中显示的常见内容以及定义主要内容部分的位置。这个看起来像:
@section('main-content')
<div class="container">
@yield('extra-content')
</div>
@endsection
Finally I got a 3rd template (test.blade.php) which extend the main template and include the common stuff I want to show, but be careful because the order matters. This one looks like:
最后,我得到了第三个模板 (test.blade.php),它扩展了主模板并包含了我想要展示的常见内容,但要小心,因为顺序很重要。这个看起来像:
@extends('main')
@section('extra-content')
<div>
<span> This is a test! </span>
</div>
@endsection
@include('common')
In your controller or your route (wherever you return your view), you should return the 3rd template.
在您的控制器或您的路线(无论您返回视图的地方)中,您应该返回第三个模板。
回答by H.Kontodios
In my projects i create some partials in order to have cleaner code and i give them as an example a name : 3show.blade.php. In order to use them in a section i just include them. I think this will do what you want.
在我的项目中,我创建了一些局部代码以获得更清晰的代码,并以它们作为示例命名:3show.blade.php。为了在一个部分中使用它们,我只是将它们包含在内。我认为这会做你想做的。
@section('content')
@include('3show.blade.php')
@endsection
回答by ??????? ????
solution 1:
you can use @show
instead of @stop
at the end of section
then laravel will render your @yield
parts ...
解决方案 1:您可以使用@show
而不是@stop
在节的末尾,然后 Laravel 将渲染您的@yield
部分...
like this :
像这样 :
@section('page-content')
<div id="content">
<article>
@yield('3show')
</article>
</div>
@show # not @stop
@section('3show')
This is a text string
@stop
this way render you view and show result so if you cll your section for twice then result will be shoed twice
这种方式呈现您查看并显示结果,因此如果您两次调用您的部分,那么结果将被插入两次
solution 2:
解决方案2:
insert call section before yiel it like this :
在屈服之前插入调用部分,如下所示:
**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**
回答by James
I had the same issue.
我遇到过同样的问题。
You can't use the @extends option in this case, you need to use @include .
在这种情况下,您不能使用 @extends 选项,您需要使用 @include 。
So lets say you have:
所以让我们说你有:
- the root layout (layouts/app.blade.php)
- an extra layout (layouts/extra.blade.php)
- the actual view that you are calling (someview.blade.php)
- 根布局(layouts/app.blade.php)
- 额外的布局(layouts/extra.blade.php)
- 您正在调用的实际视图(someview.blade.php)
The solution is to use add/inherit the root layout (1) to the top line of your view (3):
解决方案是使用添加/继承根布局 (1) 到视图 (3) 的顶行:
@extends('layouts.app')
Next, add/inherit the extra layout (2) to the second line of your view, BUT use @include not @extends:
接下来,将额外的布局 (2) 添加/继承到视图的第二行,但使用 @include 而不是 @extends:
@include('layouts.extra')
...and remember to wrap the content of your extra layout in an @section name, for example @section('extra')
...并记住将额外布局的内容包装在@section 名称中,例如@section('extra')
Finally you can call your extra layout content from your view:
最后,您可以从视图中调用额外的布局内容:
<p>Look below, you will see my extra content...</p>
@yield('extra')
So in summary, your someview.blade.php view would look like:
总而言之,您的 someview.blade.php 视图将如下所示:
@extends('layouts.app')
@include('layouts.extra')
@section('content')
<p>Look below, you will see my extra content...</p>
@yield('extra')
@endsection
回答by M.Elwan
assume you have two files:
假设您有两个文件:
-article_base.blade.php -> the default data in every article.
-article_base.blade.php -> 每篇文章的默认数据。
-article_index.blade.php -> the customized file.
-article_index.blade.php -> 自定义文件。
article_base.blade.php
@extends('layout')
@section('page-content')
<div id="content">
<article>
....
@yield('3show')
....
</article>
</div>
<!--#content-->
@stop
article_index.blade.php
article_index.blade.php
@extends('article_base')
@section('3show')
This is a text string
@endsection
I hope this works
我希望这有效
回答by tsveti_iko
You have to both @include and @yield the child template in your main template to be able to add the child template at a specific place inside the main template (not just before or after the main template - this is done by adding @parent in the child template - but in between):
您必须同时@include 和@yield 主模板中的子模板,以便能够在主模板内的特定位置添加子模板(不仅仅是在主模板之前或之后 - 这是通过在其中添加@parent 来完成的)子模板 - 但介于两者之间):
main.blade.php
主刀片.php
@include('child')
<div>
...
@yield('child')
</div>
child.blade.php
child.blade.php
@section('child')
<div>
...
</div>
@endsection