laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

@stack / @push / @prepend don't work as expected #2534

Closed smares closed 3 years ago

smares commented 3 years ago

In template.blade.php:

@push('scripts')
    <script src="{{ mix('js/manifest.js') }}"></script>
    <script src="{{ mix('js/vendor.js') }}"></script>
    <script src="{{ mix('js/app.js') }}"></script>
@endpush
<!DOCTYPE html>
<html lang="@yield('language', str_replace('_', '-', app()->getLocale()))">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>@yield('title', config('app.name'))</title>

<link href="{{ mix('css/app.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
@yield('content')
@stack('scripts')
</body>
</html>

In page.blade.php:

@extends('base.three-columns')

@section('content')
...
@ensection

@push('scripts')
    <script>123</script>
@endpush

When rendering the page, I would expect 123 to come after the scripts pushed by the template, but 123 comes first. Same happens if I have a Blade component that pushes something to scripts.

What I am actually trying to achieve is define the site-wide scripts somewhere and then use @push and @prepend to load scripts before or after the site-wide scripts. As a work-around, I will be using two stacks, something like this in the template.blade.php:

...
@stack('scripts-before')
<script src="{{ mix('js/manifest.js') }}"></script>
<script src="{{ mix('js/vendor.js') }}"></script>
<script src="{{ mix('js/app.js') }}"></script>
@stack('scripts-after')
</body>
...
smares commented 3 years ago

Dang, wrong project, sorry!