简体   繁体   中英

Blade engine in Laravel - yield doesn't work

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

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

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

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 content view, it will know that it has to extend stuff from hello .

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

对于所有那些苦苦挣扎的人,请确保你的模板php文件是带有刀片扩展名的名称,如下所示: mytemplate.blade.php ,如果你错误地退出.blade扩展名,你的模板就不会被正确解析。

You should use

@extends('layouts.master')

instead of

@extends('hello')

in your hello.blade.php view file and make sure you have a master layout in your views/layouts directory so your hello.blad.php will extend the master layout and template will show up.

Actually, your hello.blade.php file should be master.blade.php and hello.blade.php should extend this master layout so hello.blade.php should look something like this:

@extends('layouts.master')

@section('content')

<p>content check</p>

@stop

The master.blade.php file:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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