5
votes

Getting into laravel and im trying to work with blade templating but its not rendering. All my examples are coming for the laravel documentation.

UPDATE
So here is my master.blade.php file located in resources > views > master.blade.php

<!DOCTYPE html>
<html>
    <head>

        @yield('layouts.header')
    </head>
    <body>
        <div class="container">
            <div class="content">
                <div class="title">Test to Laravel 5</div>
            </div>
        </div>
    </body>
</html>

and here is my header.blade.php file located in view/layouts/

@section('header')
    <title>cookie monster</title>
    <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
    <style>
        html, body {
            height: 100%;
        }

        body {
            margin: 0;
            padding: 0;
            width: 100%;
            display: table;
            font-weight: 100;
            font-family: 'Lato';
        }

        .container {
            text-align: center;
            display: table-cell;
            vertical-align: middle;
        }

        .content {
            text-align: center;
            display: inline-block;
        }

        .title {
            font-size: 96px;
        }
</style>
@endsection

when i try to render my page no styles are being added even tho i have inline css at the moment, because i'm just testing how blade template engine works.

5
I came over here from Google scratching my head. Turns out the file name was missing the .blade.php extension for me.Shafiq al-Shaar

5 Answers

6
votes

You want to be using @include('layouts.header') rather than @yield.

@yield is what you use on a master template to specify where content will go that you can then define on a child view.

@include "allows you to easily include a Blade view from within an existing view." - https://laravel.com/docs/5.1/blade

0
votes

Your layout needs to extend something. That way it can take advantage of the defined regions in your template.

@extends('master')
0
votes

layout:

    @yield('header')
</head>
<body>
    <div class="container">
        <div class="content">
            @yield('content')
            <div class="title">Test to Laravel 5</div>
        </div>
    </div>
</body>
</html>

now, you use in controller

return view('someview');

and view code

@extend('layout')
@section('header')
<title>cookie monster</title>
@endsection
@section('content')
bla-bla-bla, render me IN content section of layout
@endsection

and result should be

<!DOCTYPE html>
<html>
<head>

<title>cookie monster</title>
</head>
<body>
    <div class="container">
        <div class="content">
            bla-bla-bla, render me IN content section of layout
            <div class="title">Test to Laravel 5</div>
        </div>
    </div>
</body>
</html>
0
votes

You should extend the header.blade.php from master.blade.php. Add the following line to the first line of header.blade.php

@extends('master')

Then you may edit the @yield. The yield and the section must be the same. For example:

master.blade.php

@yield('header')

header.blade.php (use @stop instead of @endsection)

@section('header')
    header content
@stop

pastebin.com links:

master.blade.php

header.blade.php

0
votes

I was facing a similar problem where:

layouts/user-profile.blade.php

<body>
  @yield('main')
  @yield('footer')
</body>

user-profile/create.blade.php

@extends('layouts.user-profile')

@section('main')
   <!-- include a personal-info sub-section -->
   @include($templateName)
@stop

@section('footer')
  <script></script>
@stop

The problem was with this sub-section inheritance where the need of blade's @parent was needed so all of the @section('footer') blocks were rendered in the @yield('footer')

user-profile/create/personal-info.blade.php

@section('footer')
  @parent
  // subsection scripts
@stop