Laravel - @yield 和@section 的区别?

Laravel - Difference between @yield and @section?

Laravel docs 开始,您可以使用两种方法在布局中包含 'sections':

<html>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

由于 @yield 也可以使用 @yield('section', 'Default Content') 传递一些默认内容,因此 @yield 只是 shorthand 不使用 @section 的 shorthand =17=]?

@section
    <!-- Nothing here -->
@show

还有哪些区别?

这一行消除了困惑:"Note that views which extend a Blade layout simply override sections from the layout. Content of the layout can be included in a child view using the @parent directive in a section"。

因此,如果您已经在主布局中定义了 @section,它将被覆盖,除非您在子布局的 @section.

中指定 @parent

但对于 @yield,它总是从子布局中获取该部分。这意味着它总是覆盖 @yield 部分,即使它的默认定义为 @yield('section', 'Default Content') .

希望这能消除您的困惑。如果您有更多问题,请告诉我。谢谢

基本上yield('content')就是一个标记。例如,在标签中,如果你放一个 yield('content'),你说这部分有内容的名称,顺便说一下,你可以在括号内命名任何你想要的。它不必满足。它可以是 yield('inside')。或者你想要的任何东西。

然后在要从布局页面导入 html 的 child 页面中,您只需说 section('name of the section').
例如,如果您在布局页面中将 header 标记为 yield('my_head_band') <-- 或您想要的任何其他内容,那么在 child 页面中您只需说 @section('my_head_band') .

这会将 header 从布局页面导入您的 child 页面。反之亦然,您的 body 部分在本例中被命名为内容。

希望这对您有所帮助。

简短回答:始终使用@yield,除非您想做一些更复杂的事情然后提供默认值string


长答案: 无论何时扩展 blade 模板,@yield@section .. @show 都可以选择被覆盖。你可以用 @yield 做的所有事情也可以用 @section .. @show 做,但反过来不行。这是他们的工作:

@yield('main')

  • 可以换成@section('main').. @endsection
  • 可以提供默认字符串但没有HTML!当没有提供 @section('main') .. @endsection 时,默认字符串将显示在 sub-blade-template 中。

@section('main') .. @show

  • 可以换成@section('main').. @endsection
  • 可以提供一个默认的 HTML 代码。当没有提供 @section('main') 时,默认 HTML 代码将显示在 sub-blade-template 中。
  • 可以替换为@section('main')@parent .. @endsection 并另外显示默认的HTML代码。

这里有一些例子:test.blade.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Test</title>
  </head>
  <body>
    <h1>This is a test</h1>

    @yield('mainA')
    @yield('mainB', 'This is the alternative 1')
    @yield('mainC', '<p>This is the alternative 2</p>')
    @yield('mainD', 'This is the alternative 3')

    @section('testA')
    @show

    @section('testB')
      This is the alternative 4
    @show

    @section('testC')
      <p>This is the alternative 5</p>
    @show

    @section('testD')
      <p>This is the alternative 6</p>
    @show


  </body>
</html>

这是另一个名为 testA.blade.php 的文件,它扩展了另一个 bladed 文件:

@extends('test')

@section('mainD')
  <div>
    <p>First replacement!</p>
    <hr>
  </div>
@endsection

@section('testC')
  <div>
    <p>Second replacement!</p>
    <hr>
  </div>
@endsection

@section('testD')
  @parent
  <div>
    <p>Additional content</p>
    <hr>
  </div>
@endsection

这就是结果:

最短答案:

如果要完全覆盖主布局上的子数据,请在主布局中使用 @yield

如果您想在带有 @parent 的子节点上同时使用主数据和子数据,请在主布局中使用 @section(或者覆盖主布局上的子数据,如 @yield

只是添加一些小东西,@yield 基本上定义了一个由 overwriting 数据注入的部分,如果我们的视图 @extends 父视图,它也可以工作。

现在,当我们 overwrite 时,我们会用一个新的实现完全替换一个实现,就像一家公司在意识到出现问题时可以决定 change/overwrite 其整个技术一样。

不应与 override

混淆