Laravel - @yield 和 @section 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29070456/
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 - Difference between @yield and @section?
提问by dayuloli
From the Laravel docs, you can include 'sections' inside layouts using two methods:
从Laravel 文档中,您可以使用两种方法在布局中包含“部分”:
<html>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
Since @yield
can also pass in some default content using @yield('section', 'Default Content')
, is @yield
just a shorthand for a @section
that do not uses @parent
?
由于@yield
也可以使用 传递一些默认内容@yield('section', 'Default Content')
,因此@yield
只是@section
不使用的a 的简写@parent
?
@section
<!-- Nothing here -->
@show
What other differences are there?
还有哪些不同之处?
回答by Sameer Nyaupane
This line clears out the confusion: "Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent
directive in a section".
这一行消除了混淆:“请注意,扩展 Blade 布局的视图只是覆盖了布局中的部分。布局的内容可以使用@parent
部分中的指令包含在子视图中”。
So, if you already have a @section
defined in the master layout, it will be overriden unless you specify @parent
inside the child layout's @section
.
因此,如果您已经@section
在主布局中定义了 ,它将被覆盖,除非您@parent
在子布局的@section
.
But for @yield
, it always gets the section from the child layout. That means it always overrides the @yield
part, even if it has a default defined as @yield('section', 'Default Content')
.
但是对于@yield
,它总是从子布局中获取部分。这意味着它始终会覆盖该@yield
部件,即使它的默认值定义为@yield('section', 'Default Content')
.
I hope that clears your confusion. Let me know if you have more questions. Thanks
我希望能消除你的困惑。如果您有更多问题,请告诉我。谢谢
回答by Adam
Short Answer: Always use @yield
unless you want to do something more complicated then providing a default string
.
简短回答:@yield
除非你想做一些比提供默认值更复杂的事情,否则总是使用string
。
Long Answer: Both @yieldand @section .. @showare used to be optionally overwritten whenever you extend the blade template. Everything you can do with @yieldcan also be done with @section .. @showbut not the other way around. Here is what they do:
长答案:每当您扩展刀片模板时,@ yield和@section .. @show都可以被选择性地覆盖。你可以用@yield做的一切也可以用@section .. @show来完成,但反过来不行。这是他们所做的:
@yield('main')
@yield('main')
- Can be replaced by @section('main') .. @endsection
- Can be provided with a default string but no HTML! The default string will be shown in the sub-blade-template when no @section('main') .. @endsectionis provided.
- 可以替换为@section('main') .. @endsection
- 可以提供默认字符串,但不能提供 HTML!当没有提供@section('main') .. @endsection时,默认字符串将显示在子刀片模板中。
@section('main') .. @show
@section('main') .. @show
- Can be replaced by @section('main') .. @endsection
- Can be provided with a default HTML code. The default HTML code will be shown in the sub-blade-template when no @section('main')is provided.
- Can be replaced by @section('main')@parent .. @endsectionand additionally shows the default HTML code.
- 可以替换为@section('main') .. @endsection
- 可以提供默认的 HTML 代码。当没有提供@section('main')时,默认的 HTML 代码将显示在 sub-blade-template 中。
- 可以替换为@section('main')@parent .. @endsection并另外显示默认的 HTML 代码。
Here some examples:test.blade.php
这里有一些例子:test.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<h1>This is a test</h1>
@yield('mainA')
@yield('mainB', 'This is the alternative 1')
@yield('mainC', '<p>This is the alternative 2</p>')
@yield('mainD', 'This is the alternative 3')
@section('testA')
@show
@section('testB')
This is the alternative 4
@show
@section('testC')
<p>This is the alternative 5</p>
@show
@section('testD')
<p>This is the alternative 6</p>
@show
</body>
</html>
here is another file called testA.blade.php
which extends the other bladed file:
这是另一个名为的文件testA.blade.php
,它扩展了另一个刀片文件:
@extends('test')
@section('mainD')
<div>
<p>First replacement!</p>
<hr>
</div>
@endsection
@section('testC')
<div>
<p>Second replacement!</p>
<hr>
</div>
@endsection
@section('testD')
@parent
<div>
<p>Additional content</p>
<hr>
</div>
@endsection
And that is the outcome:
这就是结果:
回答by Pradeep Sahoo
Basically yield('content')
is a marker. For example, in the tag if you put a yield('content')
, your saying this section has the name of content and by the way, you can name inside of the parenthesis anything you want. it doesn't have to be content. it can be yield('inside'). or anything you want.
基本上yield('content')
是一个标记。例如,在标签中,如果你放了一个yield('content')
,你说这个部分有内容的名称,顺便说一句,你可以在括号内命名任何你想要的。它不必满足。它可以是 yield('inside')。或任何你想要的。
And then in the child page where you want to import html from your layout page, you just say section('name of the section')
.
for example, if you have marked your header in your layout page as yield('my_head_band')
<-- or anything else you want, then in your child page you just say @section('my_head_band')
.
然后在您想要从布局页面导入 html 的子页面中,您只需说 section ('name of the section')
。
例如,如果您在布局页面中将标题标记为 yield ('my_head_band')
<-- 或任何其他您想要的内容,那么在您的子页面中您只需说@section('my_head_band')
.
This would import the header from the layout page into your child page. vice versa with your body section which in this case was named as content.
这会将标题从布局页面导入到您的子页面中。反之亦然,您的正文部分在这种情况下被命名为内容。
Hope this helps.
希望这可以帮助。
回答by mohammad asghari
The shortest answer:
最短的答案:
Use @yield
in master if you want to overwrite child data on master layout completely.
@yield
如果要完全覆盖母版布局上的子数据,请在母版中使用。
Use @section
in master if you want to use master and child data together on child with @parent
(Or overwrite child data on master layout like @yield
)
@section
在母版中使用,如果您想在子级上一起使用母版和子数据@parent
(或覆盖母版布局上的子数据,例如@yield
)
回答by Tony Okoth
Just to add on something small, @yield
basically defines a section to be injected by overwriting
the data and it also works if our view @extends
the parent view.
只是添加一些小东西, @yield
基本上定义了一个要由overwriting
数据注入的部分,如果我们查看@extends
父视图,它也可以工作。
Now when we overwrite
, we replace an implementation completely with a new implementation, like a company can decide to change/overwrite its entire technology if they realize something went wrong.
现在,当我们overwrite
用新的实现完全替换一个实现时,就像公司可以决定在他们意识到出现问题时更改/覆盖其整个技术一样。
It should not be confused with override
它不应该与 override