We can use a section to define some HTML and then yield that somewhere else.

那我们为什么要有堆栈呢?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>

推荐答案

I might be mistaken, but the difference is not only semantically, but in behaviour as well. With @push you append as many times as needed to a stack, while (by default) you may fill @section only once in 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:

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

    @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>

Laravel相关问答推荐

如何让 Laravel 的 Collection 表现得像一个流?

按回车键时如何防止此功能运行?

Laravel:在行的子集上同步多对多

不能在 laravel 中使用控制器名称,必须使用命名空间

Laravel 从 5.1 升级到 5.2.0 错误

Laravel:如何在没有数据库的情况下对用户进行身份验证

Laravel 5 - 没有重叠的任务计划不起作用

致命错误:找不到类App\Http\Controllers\Redirect

如何在同一行显示 Laravel artisan 命令输出?

如何在 Laravel 中为模型命名,以免与现有类发生冲突?

Cookie::forget 不工作 laravel 5.1

Heroku CLI 安装错误:PATH 未更新,原始长度 1585 > 1024

约定表名(带下划线)

Laravel 5 干预中的图像验证

如何使用命令行手动运行 laravel/lumen 作业(job)

在 Laravel 测试用例中模拟一个 http 请求并解析路由参数

基于日期的 Laravel 日志(log)文件

如何使用队列设置高、低和中优先级Electron邮件?

Laravel 更新查询

搜索结果分页 laravel 5.3