Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blade engine in Laravel - yield doesn't work

Tags:

php

laravel

blade

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

like image 666
Slávek Parchomenko Avatar asked Mar 18 '14 17:03

Slávek Parchomenko


2 Answers

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.

like image 200
thePHPHero Avatar answered Sep 29 '22 17:09

thePHPHero


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');
});
like image 36
totymedli Avatar answered Sep 29 '22 16:09

totymedli