@push('scripts')
<script src="/example.js"></script>
@endpush
我们可以使用section
定义一些HTML,然后在其他地方yield
定义。
那么为什么我们有栈? https://laravel.com/docs/5.2/blade#stacks
它使用不同的关键字执行的操作完全相同,但是选项较少(没有继承)。
@push('scripts')
<script src="/example.js"></script>
@endpush
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
可以使用以下部分完成:
@section('scripts')
<script src="/example.js"></script>
@endsection
<head>
<!-- Head Contents -->
@yield('scripts')
</head>
我可能会误会,但是区别不仅在语义上,而且在行为上也是如此。使用@ push,您可以将[@extends('master')
...
@for ($i = 0; $i < 3; $i++)
@push('test-push')
<script type="text/javascript">
// Push {{ $i }}
</script>
@endpush
@section('test-section')
<script type="text/javascript">
// Section {{ $i }}
</script>
@endsection
@endfor
master.blade.php
@stack('test-push')
@yield('test-section')
</body>
结果:
<script type="text/javascript">
// Push 0
</script>
<script type="text/javascript">
// Push 1
</script>
<script type="text/javascript">
// Push 2
</script>
<script type="text/javascript">
// Section 0
</script>
</body>
堆栈在某种程度上适合于脚本,有了堆栈,您可以根据需要追加任意数量。@push('scripts') <script src="/example.js"></script> @endpush
追加...
<head> <!-- Head Contents --> @stack('scripts') </head>
您可以看到,脚本堆栈将附加在example.js的script标签下。因此,您可以为每个视图推送特殊脚本。
@section - You can add your js css once. @stack - Push js css into stack (page) many times.