Question

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

Was it helpful?

Solution

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');
});

OTHER TIPS

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.

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top