Blade compiler compiles/executes @section in extended template even when overwritten by a @section in child template
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
parentMethodwill be something likehttp://example.com/users/1. - The rendered output for
childeMethodwould be"N/A", but I instead get aTrying to get property of non-objectnotice on$userfrom theviews/parent.blade.phpblade 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.
Yeah, I don't really have a good way to prevent that scenario. I would just check isset $user in your parent view.
+1 I used isset as well,
but if this is fixable somehow, we would like to know ;-)
+1
@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 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 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.
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?
Is there a solution?
+1