laravel Blade中的Section和Stack有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35672485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between Section and Stack in Blade?
提问by Webinan
We can use a section
to define some HTML and then yield
that somewhere else.
我们可以使用 asection
来定义一些 HTML,然后yield
在其他地方定义。
So why do we have stacks? https://laravel.com/docs/5.2/blade#stacks
那么为什么我们有堆栈呢? https://laravel.com/docs/5.2/blade#stacks
It's doing exactly the same thing with different keywords, but has fewer options (No inheritance).
它使用不同的关键字做完全相同的事情,但选项较少(无继承)。
@push('scripts')
<script src="/example.js"></script>
@endpush
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
Can be done with section:
可以用部分完成:
@section('scripts')
<script src="/example.js"></script>
@endsection
<head>
<!-- Head Contents -->
@yield('scripts')
</head>
回答by macghriogair
I might be mistaken, but the difference is not only semantically, but in behaviour as well. With @pushyou appendas many times as needed to a stack, while (by default) you may fill @sectiononly oncein your views. In some situations this comes in handy when you need to add content from different locations across your template files or in loops:
我可能弄错了,但差异不仅在语义上,而且在行为上。使用@push,您可以根据需要向堆栈追加多次,而(默认情况下)您只能在视图中填充@section一次。在某些情况下,当您需要跨模板文件或循环添加来自不同位置的内容时,这会派上用场:
index.blade.php:
index.blade.php:
@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
master.blade.php
@stack('test-push')
@yield('test-section')
</body>
result:
结果:
<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>
回答by Hos Mercury
Stack is someway appropriate for scripts , with stack you can Append as much as you need .
堆栈在某种程度上适用于脚本,您可以根据需要添加堆栈。
@push('scripts')
<script src="/example.js"></script>
@endpush
Append …
附加…
<head>
<!-- Head Contents -->
@stack('scripts')
</head>
As you can see the scripts stack will be appended under the script tag of example.js . So you can push special scripts for each view .
如您所见,脚本堆栈将附加在 example.js 的脚本标记下。因此,您可以为每个视图推送特殊脚本。
回答by Connectify_user
@section - You can add your js css once.
@stack - Push js css into stack (page) many times.