从Laravel文档中,您可以使用两种方法在布局中包含"部分":
<html>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
既然@yield也可以使用一些默认内容@yield('section', 'Default Content'),@yield只是一个@section不使用的简写@parent?
@section
<!-- Nothing here -->
@show
Run Code Online (Sandbox Code Playgroud)
还有什么其他差异?
Sam*_*ane 27
这一行清除了混淆:"请注意,扩展Blade布局的视图只是覆盖布局中的部分.布局的内容可以使用@parent部分中的指令包含在子视图中".
因此,如果您已经@section在主布局中定义了它,那么除非您@parent在子布局中指定,否则它将被覆盖@section.
但是@yield,它始终从子布局中获取部分.这意味着它总是覆盖该@yield部分,即使它的默认定义为@yield('section', 'Default Content').
我希望能够清除你的困惑.如果您有更多问题,请与我们联系.谢谢
Ada*_*dam 25
简答:@yield除非你想做一些更复杂的事情然后提供默认值,否则一定要使用string.
长答案:无论何时扩展刀片模板,都会选择覆盖@yield和@section .. @show.你可以用@yield做的所有事情也可以用@section .. @show来完成,但不是相反.这是他们做的:
@yield( '主')
@section('main').. @ show
这里有一些例子: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>
Run Code Online (Sandbox Code Playgroud)
这是另一个文件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
Run Code Online (Sandbox Code Playgroud)
这就是结果:
小智 5
基本上yield('content')是一个标记。例如,在标签中,如果你放了一个yield('content'),你说这个部分有内容的名称,顺便说一下,你可以在括号内命名任何你想要的东西。它不必满足。它可以是 yield('inside')。或任何你想要的。
然后在您想要从布局页面导入 html 的子页面中,您只需说 section ('name of the section')。
例如,如果您在布局页面中将标题标记为 yield ('my_head_band')<-- 或任何其他您想要的内容,那么在您的子页面中您只需说@section('my_head_band').
这会将标题从布局页面导入到您的子页面中。反之亦然,您的正文部分在这种情况下被命名为内容。
希望这可以帮助。
最短的答案:
@yield如果要完全覆盖母版布局上的子数据,请在母版中使用。
@section在母版中使用,如果您想在子级上一起使用母版和子数据@parent(或覆盖母版布局上的子数据,例如@yield)
| 归档时间: |
|
| 查看次数: |
32975 次 |
| 最近记录: |