php - laravel blade,如何附加到一个部分

标签 php laravel laravel-4 template-engine laravel-blade

如果你查看 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/

相关文章:

laravel - 如何在我的索引中搜索拼写错误的单词?

php - 编辑/更新数据库 Laravel

php - Laravel 4 总是返回 HTTP 状态码 200

php - 如何创建模式表数据类型 longtext?

php - 如何使用php以随机方式显示mysql的输出

php - SQL - 选择彼此相似的 %...% 的所有行(所有相似的行)

php - 没有模型的 Eloquent 关系

c# - 什么相当于 asp.net 中的 php 页面设置

php - 处理大量数据。 PHP session 还是每次都访问数据?

php - Laravel 将 put/patch 路由定义为相同的路由名称