Laravel 中的刀片引擎 - 产量不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22486137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:13:12  来源:igfitidea点击:

Blade engine in Laravel - yield doesn't work

phplaravelblade

提问by Slávek Parchomenko

I have been repeatedly trying to make simple blade template work. This is the code:

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

routes.php

路由文件

<?php

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

BaseController.php

基础控制器.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

hello.blade.php

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

    hello

    @yield('content')

</body>
</html>

content.blade.php

内容.blade.php

@extends('hello')

@section('content')

<p>content check</p>

@stop

When I run this code in a browser, all there is is just hello text I wrote in hello.blade.php, however yield('content') does not display anything and I can't figure out why. I'd appreciate any help, thanks

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

回答by totymedli

You created the wrong view. The parent view is hello, it doesn't know about content. That's why you wrote @extends('hello'), that way when you create your contentview, it will know that it has to extend stuff from hello.

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

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

回答by thePHPHero

For all of those struggling with this, make sure that your template php file is name with the blade extension in it, like so: mytemplate.blade.php, if you make the mistake of leaving the .blade extension out, you template will not be parsed correctly.

对于所有为此苦苦挣扎的人,请确保您的模板 php 文件的名称包含刀片扩展名,例如:mytemplate.blade.php,如果您错误地将 .blade 扩展名保留了下来,您的模板将不会被正确解析。

回答by The Alpha

You should use

你应该使用

@extends('layouts.master')

instead of

代替

@extends('hello')

in your hello.blade.phpview file and make sure you have a master layout in your views/layoutsdirectory so your hello.blad.phpwill extend the master layout and template will show up.

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

Actually, your hello.blade.phpfile should be master.blade.phpand hello.blade.phpshould extend this master layout so hello.blade.phpshould look something like this:

实际上,您的hello.blade.php文件应该master.blade.php并且hello.blade.php应该扩展此主布局,因此hello.blade.php应该如下所示:

@extends('layouts.master')

@section('content')

<p>content check</p>

@stop

The master.blade.phpfile:

master.blade.php文件:

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