Laravel 的 @yield 和 @include 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41916127/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:38:05  来源:igfitidea点击:

What's the difference between Laravel's @yield and @include?

laravel

提问by Edson Horacio Junior

I'm learning Laravel (starting at version 5.3) and those two look very much alike, the only difference I know is that @include inject parent's variables and can also send other variables.

我正在学习 Laravel(从 5.3 版开始),这两个看起来非常相似,我知道的唯一区别是 @include 注入父变量,也可以发送其他变量。

  • What's the difference between @yield and @include?
  • When should I use @yield?
  • When should I use @include?
  • @yield 和 @include 有什么区别?
  • 我什么时候应该使用@yield?
  • 我什么时候应该使用@include?

采纳答案by Rob Fonseca

@yield is mainly used to define a section in a layout. When that layout is extended with @extends, you can define what goes in that section with the @section directive in your views.

@yield 主要用于在布局中定义一个部分。当使用@extends 扩展该布局时,您可以使用视图中的@section 指令定义该部分中的内容。

The layout usually contains your HTML, head, body, header and footers. You define an area (@yield) within the layout that your pages which are extending the template will put their content into.

布局通常包含您的 HTML、头部、正文、页眉和页脚。您在布局中定义一个区域 (@yield),扩展模板的页面会将其内容放入该区域。

In your master template you define the area. For example:

在您的主模板中定义区域。例如:

<body>
     @yield('content')
</body>

Lets say your home page extends that layout

假设您的主页扩展了该布局

@extends('layouts.app')

@section('content')
     // home page content here
@endsection

Any HTML you define in the content section on your homepage view in the 'content' section will be injected into the layout it extended in that spot.

您在“内容”部分的主页视图的内容部分中定义的任何 HTML 都将注入到它在该位置扩展的布局中。

@include is used for reusable HTML just like a standard PHP include. It does not have that parent/child relationship like @yield and @section.

@include 用于可重用的 HTML,就像标准的 PHP 包含一样。它没有像@yield 和@section 这样的父/子关系。

I highly suggest reading the documentation on Blade Templates on the Laravel site for a more comprehensive overview

我强烈建议阅读 Laravel 站点上有关 Blade Templates 的文档以获得更全面的概述

https://laravel.com/docs/5.0/templates

https://laravel.com/docs/5.0/templates

回答by G-Man

@includeand @yieldare two completely different types of operations to import code into the current file.

@include@yield是两种完全不同类型的将代码导入当前文件的操作。

@include- import the contents of a separate file into the current file at the location in which it is placed. i.e.:

@include- 将单独文件的内容导入到当前文件所在位置的当前文件中。IE:

Layout file:

布局文件:

< some html or other script >

@include('include.file_name') // "include." indicates the subdirectory that the file is in

< more html or other script >

Include File( a blade file with a block of code ):

包含文件(带有代码块的刀片文件):

< some cool code here >

The contents of 'file_name' ( also a blade file ) is then imported in where the @include directive is located.

然后将'file_name'(也是一个刀片文件)的内容导入@include 指令所在的位置。

@yieldimports code from a "section" in the child file ( the "view" blade file. ) i.e.:

@yield从子文件(“视图”刀片文件)中的“部分”导入代码,即:

Layout file:

布局文件:

< some html or other script >

@yield('needed_section_name')

< more html or other script >

The following section is needed in the "view" blade file that is set to "extend" that layout file.

在设置为“扩展”该布局文件的“视图”刀片文件中需要以下部分。

"View" blade file:

“查看”刀片文件:

@extends('layout.file_name')
... code as neeeded

@section('needed_section_name')
< some cool code here >
@stop

...
more code as needed

Now the layout file will import in the section of code that matches the naming used.

现在布局文件将导入与使用的命名匹配的代码部分。

More on the subject here....

更多关于这里的主题......

回答by Steffo Dimfelt

The difference between @yieldand @includeis how you use them.

@yield和之间的区别在于@include您如何使用它们。

If you have a static kind of content, like a navbar, this part of the page will always be in the same place in the layout. When you use @includein the layout file, the navbar will be put once per layout. But if you use @yieldyou will be enforced to make a @sectionof the navbar on every page that @extendsthe layout.

如果您有静态内容,例如导航栏,则页面的这一部分将始终位于布局中的同一位置。当您@include在布局文件中使用时,导航栏将在每个布局中放置一次。但是,如果您使用,@yield您将被迫在布局的@section每个页面上制作导航栏@extends

@yieldis, on the other hand, a better choice when content is changing on all the pages but you still want to use the same layout everywhere. If you use @includeyou'll have to make a new layout for every page, because of the difference of content.

@yield另一方面,当所有页面上的内容都发生变化但您仍然希望在任何地方使用相同的布局时,这是一个更好的选择。如果您使用,@include您将不得不为每个页面制作一个新布局,因为内容不同。

回答by curiosity

for example you have already your layout structure where you @include('some scripts or style'). it will not allow you to change its directive while @yieldyou can change its content. means you create a sectionto yieldinto your layout.blade.php. you can use yield also if you have a specific script or style in each page.

例如,您已经拥有@include('some scripts or style') 的布局结构。它不允许您更改其指令,而@yield您可以更改其内容。意味着您创建一个部分屈服于您的layout.blade.php. 如果每个页面都有特定的脚本或样式,也可以使用 yield。

@include('layouts.nav') //default when you call layout.blade.php

<div class="container"> 
  @yield('content') //changes according to your view
</div>

@include('layouts.footer') //yes you can use @yield if you have specific script.