Php laravel刀片,如何附加到一个部分

Php laravel刀片,如何附加到一个部分,php,laravel,laravel-4,template-engine,blade,Php,Laravel,Laravel 4,Template Engine,Blade,如果你查阅拉威尔的官方文件 它说给这个布局: <!-- Stored in app/views/layouts/master.blade.php --> <html> <body> @section('sidebar') This is the master sidebar. @show <div class="container"> @yi

如果你查阅拉威尔的官方文件 它说给这个布局:

<!-- 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
@extends('layouts.master'))
@节(“侧栏”)
这将附加到主侧边栏

@停止 @节(“内容”) 这是我的身体内容

@停止
将附加到部分
侧栏
。但实际上,如果您尝试的是它不附加,它只是覆盖扩展模板中的内容

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

此外,官方文档中的这个例子不起作用,我发现刀片文档非常糟糕。例如,blade函数没有类似于
@parent
的功能。

中的示例似乎确实有缺陷,但我认为这是网站上的降价解析问题,显示正确的代码:

无论如何,
@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
/**
 * 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;
}

您只需使用
@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
依次插入对
appendSection()
的调用,如下所示:

@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
/**
 * 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;
}

如前所述,我使用了
@parent
,对我来说效果很好。例如,扩展的
标题将有助于:

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>
<meta charset="utf-8">
<title>@yield('title')</title>
@extends('master')

@section('title')
@parent
| {{$post->title }}
@stop
@section('content')
// Post Body here ..
@stop
...
@section('title', 'My Blog')
...
...
@section('title', '@parent | ' . $post->ar_name )
...
因此,标题将呈现如下:

@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
/**
 * 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;
}
我的博客|我的帖子标题


实际上,这将呈现如下内容:

<title>
    My Blog
    | My Post Title
</title> 
post.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>
<meta charset="utf-8">
<title>@yield('title')</title>
@extends('master')

@section('title')
@parent
| {{$post->title }}
@stop
@section('content')
// Post Body here ..
@stop
...
@section('title', 'My Blog')
...
...
@section('title', '@parent | ' . $post->ar_name )
...
这将导致:

<title>My Blog | My Post Title</title> 
我的博客|我的帖子标题
所以你要去掉标题里面的行

希望这有帮助

注:
这是用于Laravel 5.2的,不太确定,但我记得,它也适用于Laravel 4。

我同意您的观点,从成熟度的角度来看,刀片模板引擎缺乏很多,这反映在非常缺乏的文档中。我个人认为这是Laravel(为数不多)的弱点之一,幸运的是,如果您想避免,可以插入另一个模板引擎,如Smarty。我决定采用这种方法(看看我前面的问题)。刀片用户似乎对它很满意,所以也许有人也会调查你的问题。@jbx使用另一个模板引擎,我认为这是个好主意。我认为symfony框架中流行的小树枝似乎是laravel项目的一个好选择,即使它很难学习。Twig也比blade好,因为您几乎可以在任何php项目中使用它,而不仅仅是与laravel绑定。对于smarty来说,我只是觉得它的官方网站很糟糕。如果你找到了一个好的Laravel供应商,你可以使用它,如果你喜欢的话。我更喜欢Smarty,因为我对它有很多经验,而且我更喜欢语法而不是Twig(我讨厌double curly
{{
以及与
{%
等的不一致性),但它肯定比Blade更成熟。我发现Smarty的文档非常全面(与Blade相比,它是一个完全不同的联盟).所有的函数都有很好的文档说明,并附有完整的解释和示例。可能该网站有点过时(我认为它是最古老的PHP模板引擎之一)但它得到了一切。这就是为什么我说,总体而言,Laravel的文档仍然很差,不仅仅是刀片组件。你会发现自己潜入源代码,看看如何使用函数!不是每个人都愿意这样做。甚至源代码中的注释都非常简洁。你必须是Laravel方面的专家才能理解还有它们。@Amaynut我认为这是一个视角的问题。我同意文档中缺少一些地方,但我不会说它很差。此外,深入源代码并不是一件痛苦的事,事实上我认为这是一件好事,每个致力于学习新工具的开发人员都应该尝试理解它作为bes的工作原理t尽可能地(没有人可以期望从一开始就成为专家,但是有一些编码经验的人应该不会觉得这让人望而生畏)@Bogdan这是一个非常偏颇的观点,根本不是一件“好事”。当你提供一个框架时,你不应该把它扔到那里,期望人们阅读代码才能使用它。它破坏了封装、重用和基本软件工程原则的全部观点。显然,这是开源的,而且它是免费的h不附加任何条件会降低作者的承诺水平(如果我没有弄错的话,目前这仍然是一个主要人物)。但这并不是突然让它变得“好”,文档化的API仍然比未文档化的要好。@jbx我想你误解了。我不是说有好的文档并不重要。我只是指出了开源的魔力,如果你想了解一些东西的内部工作原理,你可以,但你不能这么说这“根本不是一件好事”@Bogdan,是的,是的,当然,我决不想贬低这家伙在Laravel上所做的工作。我认为这太棒了。但是,我不同意一些人的观点,即你只是把一些东西扔出去,期望人们从中理解。代码可以是数千行,而简单描述几行函数是如何实现的行为足以让用户继续使用它。除了可能存在错误,以及预期记录的行为与实际实现之间的不匹配之外,这使人们能够识别并修复它。它不起作用,追加只会覆盖父部分。为什么以前不尝试解决方案呢发帖?它确实有效,我目前正在我的几个项目中使用它。正如泰勒所说,它是在Laravel4.1中添加的,并且仍然在Laravel5的开发分支中工作。它有效