【问题标题】:Blade engine in Laravel - yield doesn't workLaravel 中的刀片引擎 - 产量不起作用
【发布时间】:2014-04-24 12:48:27
【问题描述】:

我一直在反复尝试制作简单的刀片模板。这是代码:

routes.php

<?php

Route::get('/', function()
{
    return View::make('hello');
});

BaseController.php

<?php

class BaseController extends Controller {

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    protected function setupLayout()
    {
        if ( ! is_null($this->layout))
        {
            $this->layout = View::make($this->layout);
        }
    }

}

hello.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>swag</title>
</head>
<body>

    hello

    @yield('content')

</body>
</html>

content.blade.php

@extends('hello')

@section('content')

<p>content check</p>

@stop

当我在浏览器中运行此代码时,只有我在 hello.blade.php 中编写的 hello 文本,但是 yield('content') 没有显示任何内容,我不知道为什么。非常感谢您的帮助,谢谢

【问题讨论】:

    标签: php laravel blade


    【解决方案1】:

    你应该使用

    @extends('layouts.master')
    

    而不是

    @extends('hello')
    

    在您的hello.blade.php 视图文件中,并确保您的views/layouts 目录中有一个主布局,以便您的hello.blad.php 将扩展主布局并显示模板。

    实际上,您的hello.blade.php 文件应该是master.blade.phphello.blade.php 应该扩展这个主布局,所以hello.blade.php 应该看起来像这样:

    @extends('layouts.master')
    
    @section('content')
    
    <p>content check</p>
    
    @stop
    

    master.blade.php 文件:

    <!DOCTYPE html>
    <html>
        <head>
           <title>swag</title>
        </head>
        <body>
            @yield('content')
        </body>
    </html>
    

    【讨论】:

    • 试过了,改了文件名、扩展名、路由等,可惜还是不行
    • 好好尝试一下,你可能犯了错误,这个答案没有错,如果你做得好,它会起作用。还有check the documentation.
    • 这是一个非常难以理解的过程解释,您应该在 Blade 文件中将所有 hello 替换为 master 并将所有 content 替换为 hello,包括它们的文件名。
    【解决方案2】:

    您创建了错误的视图。父视图是hello,它不知道content。这就是你写@extends('hello')的原因,这样当你创建content视图时,它就会知道它必须从hello扩展东西。

    Route::get('/', function()
    {
        return View::make('content');
    });
    

    【讨论】:

    • 哦,天哪,谢谢,我的想法是,因为我将主视图设置为默认值,它会检查是否应该添加任何内容,然后显示所有产量和包括。看来我错了,这行得通。再次感谢!
    【解决方案3】:

    对于所有为此苦苦挣扎的人,请确保您的模板 php 文件的名称中包含刀片扩展名,例如:mytemplate.blade.php,如果您犯了离开的错误.blade 扩展名,您的模板将无法正确解析。

    【讨论】:

      猜你喜欢
      • 2018-05-28
      • 2019-01-31
      • 2019-01-12
      • 2014-08-14
      • 2014-11-15
      • 2018-05-22
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多