I'm working in Laravel 5.2. Everything seems to be working as designed, but not as I'd like.
• I have a 'master' template, which is extended by all other templates:
@include('template.head')
<p>Yeah! I’m a master template!</p>
@yield('content')
@include('template.footer')
• I have a test template, called 'dashboard', which also @includes another template, called 'widgets':
@extends('template.master')
@section('content')
This is the content for 'dashboard'
@include('template.widgets')
@endsection
• Content of 'widgets' uses the @parent tag to avoid overwriting anything already added to the 'content' section:
@extends('template.master')
@section('content')
@parent
This is a list of widgets
@endsection
Imagine 'widgets' contains a list of widgets, that I can access through a 'widgets' controller. In addition, I have a 'dashboard' view. I want to include that list of widgets, along with some other data. My 'dashboard' page could theoretically include other views that are also used as 'stand-alone' views by their respective controllers. I might have 'widget inventory' and 'widget sales' for instance.
The problem is that in the current system, Blade will output each '@include' in a template every time that template is 'extended'. So in this case, if I include several templates that extend the master template, I get several outputs of the 'head' and 'footer', in various places on the page. This makes total sense, but isn't helpful for my scenario.
What I would like to add to Blade is an @includeOnce
tag. This would create an array to hold the names of sections tagged with 'includeOnce'. The first time each piece of content is pulled in with '@includeOnce', the content would be added to the output for that section and the name of that include would be added to the 'includedOnce' array. Future requests for that same named content could then be ignored.
This would allow all sub-templates to '@extend' the master template, and be used as stand-alone views for their respective controllers. They could ALSO be included in other views (like a dashboard, or any other view that needed to composite data from multiple controllers) — without any modifications. Content tagged with '@includeOnce' would be output only once, even if it was called multiple times by various sub-templates.
So my question (at long last): Is extending Blade in this way a good idea? Or has someone already come up with a better / cleaner workaround for this problem? Secondly, does anyone see a problem with my general logic of using an array to track these one-time includes? There is a lot that I don’t know about the guts of Laravel / Blade, so I’m wondering if this is at all workable...
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire