laravel blade @stack 要放在 @include 之後才能正常使用
直接用範例來說明,目前我們有下面兩個 blade,分別是 layout 和 content。
範例:主視圖(layout.blade.php)
範例:主視圖(layout.blade.php)
<html> <head> @stack('scripts') </head> <body> @include('content') </body> </html>範例:子視圖(content.blade.php)
@push('scripts') <script src="footer.js"></script> @endpush這種情況下會造成子視圖 @push 沒有作用,起因在於程式解析的順序是從上到下,@stack 只會輸出當前已經 push 進來的內容。
- 在主視圖解析到 @stack 時子視圖還沒載入
- 解析到 @include 時,雖然子視圖有寫 @push,但是 @stack 已經解析完成,不會再新增內容。
<html> <head> </head> <body> @include('content') @stack('scripts') </body> </html>
留言
張貼留言