laravel刀片,如何附加到一节 [英] laravel blade, how to append to a section

查看:56
本文介绍了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.

推荐答案

Larvel网站的文档中的示例确实确实存在缺陷,但我认为这是网站上的降价解析问题,

The example in the documentation from Larvel 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;
}

这篇关于laravel刀片,如何附加到一节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆