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

Ama*_*nut 29 php template-engine laravel blade laravel-4

如果您查看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>
Run Code Online (Sandbox Code Playgroud)

通过这种观点扩展

@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
Run Code Online (Sandbox Code Playgroud)

将附加到该部分sidebar.但实际上如果你尝试它不会附加,它只是覆盖扩展模板中的内容.

我听说过其他刀片功能@append, @prepend, @parent......似乎没有人工作.

除此之外,官方文档中的这个例子不起作用,我发现刀片文档非常差.例如@parent,刀片功能没什么.

Bog*_*dan 49

Larvel网站文档中的示例确实看起来确实存在缺陷,但我认为这是网站上的降价解析问题,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
Run Code Online (Sandbox Code Playgroud)

快速浏览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;
}
Run Code Online (Sandbox Code Playgroud)

  • 这就是为什么我说对于Laravel而言,文档仍然很差,不仅限于刀片组件。您会发现自己陷入了源代码中,以了解如何使用函数!并非每个人都愿意这样做。甚至源代码中的注释也非常简洁。您需要成为Laravel的专家才能理解它们。 (2认同)
  • @Bogdan,是的,当然是的,我绝不应该贬低他在Laravel上所做的工作。我认为它很棒。但是,我不同意某些人的观点,即您只是将某些东西扔在那里,并期望人们从中了解。该代码可以是数千行,而仅几行描述一个函数的行为方式就足以让用户继续使用它。除了可能存在错误的事实外,还会出现预期的行为记录与实际实现之间的不匹配,这使人们能够识别并修复它。 (2认同)

use*_*364 20

你可以简单地使用@append......

@extends('layouts.master')

@section('sidebar')
    <p>This is appended to the master sidebar.</p>
@append

@section('content')
    <p>This is my body content.</p>
@stop
Run Code Online (Sandbox Code Playgroud)

看到这里.

要了解这是如何工作的......

compileStatements()在BladeCompiler方法调用的方法compileAppend(),你可以在这里看到:

/**
 * Compile Blade Statements that start with "@"
 *
 * @param  string  $value
 * @return mixed
 */
protected function compileStatements($value)
{
    $callback = function($match)
    {
        if (method_exists($this, $method = 'compile'.ucfirst($match[1])))
        {
            $match[0] = $this->$method(array_get($match, 3));
        }

        return isset($match[3]) ? $match[0] : $match[0].$match[2];
    };

    return preg_replace_callback('/\B@(\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
}
Run Code Online (Sandbox Code Playgroud)

反过来,插入一个如下所示的调用appendSection():

/**
 * Stop injecting content into a section and append it.
 *
 * @return string
 */
public function appendSection()
{
    $last = array_pop($this->sectionStack);

    if (isset($this->sections[$last]))
    {
        $this->sections[$last] .= ob_get_clean();
    }
    else
    {
        $this->sections[$last] = ob_get_clean();
    }

    return $last;
}
Run Code Online (Sandbox Code Playgroud)

  • 它不起作用,追加只会覆盖父部分。为什么你不在发布之前尝试你的解决方案? (2认同)