Override section in a laravel blade template throwing undefined variable errors Override section in a laravel blade template throwing undefined variable errors laravel laravel

Override section in a laravel blade template throwing undefined variable errors


Blade layouts work their way up the tree to the route or master view before rendering any of the children. Thus, nested views that extend others must always have their parent rendered before they are. As a result, parent views that contain sections will always be rendered prior to the child.

To overcome the problem you are experiencing it is a good idea to only ever nest pages that don't overwrite parents sections that contain variables, as the parents content will always be rendered before the child.

As the above ideal can't always be adhered to or a parents section content is still required, a good alternative method would be to use view composers. View composers give you an opportunity to declare variables for any specific view whenever they are rendered.

View::composer(array('pages.admin'), function($view){    $view->with('name', Auth::user()->username);});

Another alternative would be to use a view creator. Creators are fired the moment a view is instantiated rather than rendered. This method allows you to overwrite the variable should you so wish prior to the view being rendered.

View::creator(array('pages.admin'), function($view){    $view->with('name', Auth::user()->username);});

You can read up more about these methods in the documentation here. (Or here for the Laravel 5 documentation.)


I can't guarantee support for Laravel 4 but for those looking for a solution that works in Laravel 5.5 (and probably a fair bit further back – hard to check) is to define the variables you need when @extending.

E.g. in the example in the question:

@extend('my.parent.view', ['name' => ''])

This approach can be especially useful if the data needed is available to the child-view, but under a different name.

E.g. if a parent-view needed a $parent variable, but the child view only has a $child variable which has a property referencing the parent, you might do:

@extend('my.parent.view', ['parent' => $child->parent])


I am not sure if this is a bug or intentional, but it seems like Laravel renders the variables before interpreting blade instructions.The workaround would be this:

views/layouts/testlayout.blade.php:

    <html>    <body>        @yield('sidebar', 'This is the master sidebar. {{ $name }}' )        <div class="container">            @yield('content')        </div>    </body>   </html>

actual view: views/test.blade.php

@extends('layouts.testlayout')@section('sidebar')   No variable in the child@stop@section('content')    This is a child content@stop

This prevents the variable $name to get rendered if there is a section with that name in the actual view. It seems like this is the approach if the content in the layout file contains a variable