如果你查看 laravel 官方文档 http://laravel.com/docs/4.2/templates 它说给这个布局:
<!-- Stored in app/views/layouts/master.blade.php -->
<html>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
由这个 View 扩展
@extends('layouts.master')
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@stop
@section('content')
<p>This is my body content.</p>
@stop
将附加到 sidebar
部分。但实际上,如果您尝试它不追加,它只是覆盖扩展模板中的内容。
我听说过其他 blade 函数,例如 @append、@prepend、@parent
……似乎没有一个起作用。
此外,官方文档中的这个示例不起作用,我发现 Blade 文档非常糟糕。例如 @parent
之类的 blade 函数没有任何意义。
最佳答案
documentation from Laravel website 中的示例确实看起来有缺陷,但我认为这是网站上的 Markdown 解析问题,same docs on github显示正确的代码:
无论如何 @parent
确实有效。文档中的示例应如下所示:
@extends('layouts.master')
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@stop
@section('content')
<p>This is my body content.</p>
@stop
快速查看 Illuminate/View/Factory.php
可以确认 @parent
的作用:
/**
* Append content to a given section.
*
* @param string $section
* @param string $content
* @return void
*/
protected function extendSection($section, $content)
{
if (isset($this->sections[$section]))
{
$content = str_replace('@parent', $content, $this->sections[$section]);
}
$this->sections[$section] = $content;
}
关于php - laravel blade,如何附加到一个部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26615793/