Support the ongoing development of Laravel.io →
posted 8 years ago
Blade
Last updated 1 year ago.
0

Just an idea: maybe it's because you've used @endsection in your register template. Have you tried @stop there too?

And: Is it the same if you switch orders (e.g. include card.files first, does it get shown as files?)?

Last updated 8 years ago.
0

If I change the order, whichever 'card' I put at the top is the one that is repeated.

I think it has something to do with

vendor/laravel/framework/src/Illuminate/View/Factory.php

in the method extendSection(). There's this snippet of code:

        if (isset($this->sections[$section])) {
            $content = str_replace('@parent', $content, $this->sections[$section]);
        }

and if I remove it, the sections don't repeat, although there is a lot of weirdness that happens with the layout, though I'm not entirely certain why.

My belief is that what is happening is once a section is compiled, it's put into an array with the key being that section's name.

If I have subsequent views with that section name (since they're extending the same master template), that check to see if that section is already set will prevent the new data from being compiled, which is why only my first template is showing.

What I think needs to happen is instead of $this->sections[$section], there needs to be something like $this->sections[$activeTemplate][$section] in order to essentially namespace the template data.

0

The problem you’re going to have is that the way @section works. It stores the section in an array based on the name provided. Once it is created it is not overwritten, you can append it with the @append but not replace. (or at least I have not found a way to do that )

The other problem is that the section name is technically a template token, so if you namespace it as you mentioned, then you end up with unique token names for every section which would be a different problem than you have now.
If you namespace the @section names, for example the view1 template would make a section named “view1.title” which means the @extends template “card” would have to have a yield named “view1.title” for it to show correctly. That breaks view2 since the parent template now has namespaced tokens.

The only way I have come up with to stack templates is make the sub templates in the controller then pass them to the view.

Route::get('/', function () {
    // used custom blade extension to turn off the escaping on demand
    Blade::setContentTags('{{', '}}', false);    
   // make sub views
    $view1 = view('view1');    
    $view2 = view('view2');        
  // assemble the final template
    return view('welcome', ['view1' => $view1, 'view2' => $view2 ]);
});

Changing the base template,

<!DOCTYPE html>
<html>
    <head>
        <title>Blade issues</title>
    </head>
    <body>
        <div class="container">
            <div class="content">
                {{$view1}}
                {{$view2}}
            </div>
        </div>
    </body>
</html>

The tokens will need to be un-escaped or any html is automatically escaped which will mess up the display. That is made difficult in the current Laravel view code. I tried to post a adjustment but I got brushed aside (or at least that was how I felt).

Therefore, I wrote my own extension to replace the default view, StringBladeCompiler, (v3). This version includes the string blade compiler and to allow the ability to turn on and off the tag escaping whenever I want, globally or per each view call.

Let me know if you have questions.

Hope that helps.

edit: forgot the link to the StringBladeCompiler, https://github.com/TerrePorter/StringBladeCompiler/tree/3.0

Last updated 8 years ago.
0

Humm.. now it's not working ...

0

Ok, so I copied the wrong route.

I had to actually fully render the subviews.

Route::get('/', function () {

    // used custom blade extension to turn off the escaping on demand
    Blade::setContentTags('{{', '}}', false);

    // make sub views
    $view1 = view('view1')->render();
    $view2 = view('view2')->render();

    // assemble the final template
    return view('welcome', ['view1' => $view1, 'view2' => $view2 ]);

});

The alternative is to make the card be a template and pass vars to it

Route::get('/test1', function () {

    // used custom blade extension to turn off the escaping on demand
    Blade::setContentTags('{{', '}}', false);

    // make sub views
    $view1 = view('card1', ['title' => 'title 1', 'content' => 'Content for card 1']);
    $view2 = view('card1', ['title' => 'title 2', 'content' => 'Content for card 2']);

    // assemble the final template
    return view('welcome', ['view1' => $view1, 'view2' => $view2 ]);

});

// card1
<h1>
{{$title}}
</h1>
<p>
{{$content}}
</p>
0

The solutions is mentioned in this card https://github.com/laravel/framework/issues/1058

Taylor use @overwrite.

@section('stuff')
     Stuff goes here...
@overwrite
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rogue780 rogue780 Joined 13 Oct 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.