简体   繁体   English

Laravel Blade,如何附加到一个部分

[英]laravel blade, how to append to a section

If you look to laravel official documentation http://laravel.com/docs/4.2/templates It says that giving this layout:如果你看 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>

Extended by this 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

Will append to the section sidebar .将附加到部分sidebar But actually if you try is it doesn't append, it just override the content from the extended template.但实际上,如果您尝试它不附加,它只是覆盖扩展模板中的内容。

I heard about others blade function like @append, @prepend, @parent ... no one seems to work.我听说其他刀片功能,如@append, @prepend, @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.例如,没有像@parent这样的刀片功能。

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: Laravel 网站文档中的示例确实似乎有缺陷,但我认为这是网站上的降价解析问题, github 上相同文档显示了正确的代码:

In any case @parent does indeed work.无论如何@parent确实有效。 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

A quick look in the Illuminate/View/Factory.php confirms what @parent does:快速查看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;
}

You can simply use @append ...您可以简单地使用@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

See here . 见这里

To understand how this works...要了解这是如何工作的...

The compileStatements() method in the BladeCompiler calls the method compileAppend() , as you can see here: compileStatements()方法调用方法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);
}

In turn, that inserts a call to appendSection() which looks like this:反过来,这会插入对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;
}

as mentioned before, I used @parent and it works fine for me.如前所述,我使用了@parent ,它对我来说很好用。 May be an example for extended title will helps:可能是扩展title的示例将有助于:

master.blade.php master.blade.php

@section('title')
My Blog 
@stop
<!doctype html>
<html>
<head>
    @include('includes.head')
</head>
<body>
<div class="container-fluid">
    <div id="main" class="row">
            @yield('content')
    </div>
</div>
</body>
</html>

includes/head.blade.php包括/head.blade.php

<meta charset="utf-8">
<title>@yield('title')</title>

post.blade.php post.blade.php

@extends('master')

@section('title')
@parent
| {{$post->title }}
@stop
@section('content')
// Post Body here ..
@stop

Therefore, The Title will be rendered to be like this:因此,标题将呈现为如下所示:

My Blog |我的博客 | My Post Title我的帖子标题


Actually, this will render something like:实际上,这将呈现如下内容:

<title>
    My Blog
    | My Post Title
</title> 

so you can use the section second parameter to set the values:因此您可以使用第二个参数部分来设置值:

includes/head.blade.php包括/head.blade.php

...
@section('title', 'My Blog')
...

post.blade.php post.blade.php

...
@section('title', '@parent | ' . $post->ar_name )
...

And this will render:这将呈现:

<title>My Blog | My Post Title</title> 

So you will get rid of the lines inside the title,所以你将摆脱标题内的行,

Hope that's helps.希望这有帮助。

Note: This is used for Laravel 5.2, Not quite sure but as I remember, it works for Laravel 4 too.注意:这用于 Laravel 5.2,不太确定,但我记得,它也适用于 Laravel 4。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM