jeudi 1 juin 2017

Laravel Blade Composor/Creator override

I am intending to provide a default set of variables to various blade views with the ability to override them later. For example

a.blade.php

<p>a.blade</p>
<p></p>
@include('b')
@include('b', ['bar' => 'bar as param'])

b.blade.php

<p>b blade</p>
<p></p>

When I use View::creator, it allows me to use ->with() at runtime to recompose my views and override the ones bound in the creator. If I use View::composer, this is not the case.

So given the below creator

View::creator('a', function ($view) {
    $view->with(['foo' => 'foo as CREATED']);
});

I am able to then use (in a route Closure for example)

return view('a')->with('foo', 'foo from composition');

// Expected a.blade foo from composition (excluding inclusion of b)
// Actual   a.blade foo from composition (excluding inclusion of b)

But I am not able to use

return view('a', ['foo' => 'foo from composition']);

// Expected a.blade foo from composition (excluding inclusion of b)
// Actual   a.blade foo as CREATED (excluding inclusion of b)

This isn't so much of an issue here as I can use ->where() but my problem lies in the fact that I can no longer use blade directives @include or @component within my templates.

So given the above example, add a creator for a

View::creator('b', function ($view) {
    $view->with(['bar' => 'bar AS CREATED']);
});

Then in a.blade.php as above

@include('b')
@include('b', ['bar' => 'bar as param'])

// Expected
//    b.blade bar AS CREATED
//    b.blade bar as param

// Actual
//    b.blade bar AS CREATED
//    b.blade bar AS CREATED

So I'm looking for a way I can achieve the ability to provide defaults for variables to views and then override them with these syntax

view('a', ['foo' => 'bar']);
@include('a', ['foo' => 'bar'])
@component('a', ['foo' => 'bar'])



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2reuenM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire