Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

@push is not working when used in @included partial view #864

Closed
ajcastro opened this issue Nov 4, 2017 · 7 comments
Closed

@push is not working when used in @included partial view #864

ajcastro opened this issue Nov 4, 2017 · 7 comments

Comments

@ajcastro
Copy link

ajcastro commented Nov 4, 2017

Laravel Version: 5.4.32
Example, this doesn't work:

layouts/app.blade.php

<html>
<head>
@stack('styles`)
</head>
<body>
  @include('navbar')
</body>
</html>

navbar.blade.php

@push('styles')
// some css styles for navbar
@endpush
<nav>
<!-- some-html-for-navbar -->
</nav>

But if you bring the @stack('styles') after @include it will work.

<htmll>
<head>
</head>
<body>
  @include('navbar')
  @stack('styles`)
</body>
</html>

But we want the styles should be in <head>

@sisve
Copy link

sisve commented Nov 5, 2017

This is intentional. The @stack is rendered before the @include executes to @push, before anything is added to it.

@OwenMelbz
Copy link

@sisve whether its intentional or not it's not intuitive or friendly - which is one of the key USPs for laravel.

What's the point of being able to push things to it if you can only use them after the rendered output.

Not saying if it should/shouldn't be re-thought to allow this functionality - however counter-productive responses don't help - might as well say "how can i stop google tracking me - then replying with - that's how google works" - something more helpful and beneficial helps everybody.


@ajcastro What you're proposing does make complete sense, and although it does not currently work like this are you in a position where you could submit a PR for review?

@sisve
Copy link

sisve commented Nov 6, 2017

Well, all the code is executed from top to bottom, and the @stack is before the @push. The blade files works just as php files in this regard, and you cannot render stuff before they are calculated. To change this behavior we would need to introduce some kind of two-stage rendering, where the @include is executed in the first stage, and the @stack is executed in the second stage.

Laravel 5.4 is no longer supported, but I'm pretty sure that this behavior is still the same in Laravel 5.5. If this is implemented you would need to upgrade to Laravel 5.5 to take part of the changes.

@Miguel-Serejo
Copy link

It looks like you're trying to use @stack as if it were @yield.

As far as I understand it, the @stack directive should be used to render things that have been @pushed beforehand. If you want a placeholder for things that haven't been defined yet, @yield should work just fine.
layouts/app.blade.php

<html>
<head>
@yield('styles`)
</head>
<body>
  @include('navbar')
</body>
</html>

navbar.blade.php

@section('styles')
// some css styles for navbar
@endsection
<nav>
<!-- some-html-for-navbar -->
</nav>

@cadicvnn
Copy link

The reason we use @stack('styles') is we need to push to 'styles' section multiple times? We want to split the layout into multiple components and each component push it's own 'styles'.
If we use @yield, we can only push once, is that right?

@sisve
Copy link

sisve commented Mar 20, 2018

Sections can append by using the @parent directive.

@section('styles')
    @parent
    
    <style type="text/css">
         * { outline: 1px solid red; }
    </style>
@endsection

@lesichkovm
Copy link

Its laravel 6 now, and stacks still not working from partial views included with @include.

It cannot be one way or another. It should either work in all cases, or removed as a feature.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants