framework icon indicating copy to clipboard operation
framework copied to clipboard

Blade compiler compiles/executes @section in extended template even when overwritten by a @section in child template

Open deefour opened this issue 13 years ago • 9 comments

Given the following UsersController actions

function show(User $user) { }

function parentAction() {
  $user = User::find(1);

  return View::make('parent', array('user' => $user));
}

function childAction() {
  return View::make('child');
}

and the following template at views/parent.blade.php

@section('test')
  {{ URL::action('UsersController@show', array('user' => $user->id)) }}
@show

and the following template at views/child.blade.php

@extends('parent')

@section('test')
  N/A
@stop
  • The rendered output for parentMethod will be something like http://example.com/users/1.
  • The rendered output for childeMethod would be "N/A", but I instead get a Trying to get property of non-object notice on $user from the views/parent.blade.php blade template.

When I omit @parent from the contents of the 'test' section in the views/child.blade.php my expectation is that the section would overwrite the extended section not only in terms of rendered output, but also for compilation, preventing the 'test' section in views/parent.blade.php from being interpreted/compiled at all.


Is what I'm seeing really the intended behavior? This causes more than just a notice to be thrown in other circumstances, and seems wasteful for the extended template to have it's sections compiled, only to immediately be overwritten in any case where @parent isn't included in the child's implementation of the same section.

(Though I don't know if it's possible) I'd really like @extends to function as PHP's extends does, where @parent in a section is the equivalent of me calling up to the parent class' method, and when omitted the parent class' method is never executed.

deefour avatar May 22 '13 16:05 deefour

Yeah, I don't really have a good way to prevent that scenario. I would just check isset $user in your parent view.

taylorotwell avatar May 22 '13 21:05 taylorotwell

+1 I used isset as well, but if this is fixable somehow, we would like to know ;-)

hannesvdvreken avatar May 31 '13 11:05 hannesvdvreken

+1

jenssegers avatar May 31 '13 11:05 jenssegers

@taylorotwell Why just save all sections as function in special table?

For example:

<?php
$arr = array();

$arr['xxx'] = function(){   // @section('xxx')
?>
    <h1> first </h1>
<?php
};  // @stop

$arr['xxx'] = function(){  //@section('xxx')
?>
    <h1> second </h1>
<?php
};  // @stop

$arr['xxx']();  // @yield('xxx')
?>

it can helps avoiding unnecessary calls to overwritten sections.

ont avatar Aug 25 '14 10:08 ont

@ont because you could call @parent from the second xxx and it would still load the first xxx section.

http://laravel.com/docs/templates#blade-templating

crynobone avatar Aug 25 '14 12:08 crynobone

@crynobone well, but it is not a problem:

<?php
$arr['xxx'] = null;


$closure = function($parent){  //@section('xxx')
    return function() use ($parent) {
?>
        <h1> first </h1>
<?php
    };
};
$arr['xxx'] = $closure($arr['xxx']); // @stop



$closure = function($parent){  //@section('xxx')
    return function() use ($parent) {
?>
        <?php $parent(); //@parent ?>
        <h1> second </h1>
<?php
    };
};
$arr['xxx'] = $closure($arr['xxx']); // @stop



$closure = function($parent){  //@section('xxx')
    return function() use ($parent) {
?>
        <?php $parent(); //@parent ?>
        <h1> third </h1>
<?php
    };
};
$arr['xxx'] = $closure($arr['xxx']); // @stop



$arr['xxx']();  // @yield('xxx')
?>

Here we chain each section together with closures.

ont avatar Aug 25 '14 14:08 ont

sorry for responding on an old topic but I'm encountering this as well and it is sometimes definitely causing a slower response because of the initial compilation that is unnecessary for the end-user. Somebody managed to solve this? maybe a different blade structure?

tomvo avatar Jul 30 '15 10:07 tomvo

Is there a solution?

tutNichts avatar Oct 04 '16 11:10 tutNichts

+1

kobykorman avatar Feb 23 '17 20:02 kobykorman