laravel 刀片,如何附加到一个部分
如果你查看 laravel 官方文档 http://laravel.com/docs/4.2/templates它说给这个布局:
If you look to laravel official documentation http://laravel.com/docs/4.2/templates It says that giving this layout:
<!-- 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>
由这个视图扩展
@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
部分.但实际上,如果您尝试它不会追加,它只是覆盖扩展模板中的内容.
Will append to the section sidebar
. But actually if you try is it doesn't append, it just override the content from the extended template.
我听说过其他刀片功能,例如 @append、@prepend、@parent
...似乎没有人工作.
I heard about others blade function like @append, @prepend, @parent
... no one seems to work.
此外,官方文档中的这个例子不起作用,我发现刀片文档很差.例如,没有像 @parent
这样的刀片功能.
Beside, this example in the official doc which doesn't work, I find that the blade documentation is very poor. There's nothing about blade function like @parent
for instance.
推荐答案
Laravel 文档中的例子website 确实似乎有缺陷,但我认为这是网站上的 Markdown 解析问题,github 上的相同文档 显示正确的代码:
The example in the documentation from Laravel website does indeed seem to be flawed, but I think it's a markdown parsing problem on the website, the same docs on github show the correct code:
在任何情况下 @parent
确实有效.文档中的示例应如下所示:
In any case @parent
does indeed work. The example in the docs should look like this:
@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
的作用:
A quick look in the Illuminate/View/Factory.php
confirms what @parent
does:
/**
* 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;
}
相关文章