什么';Laravel和#x27之间的区别是什么;s@收益率和@include?

什么';Laravel和#x27之间的区别是什么;s@收益率和@include?,laravel,Laravel,我正在学习Laravel(从5.3版开始),这两个版本看起来非常相似,唯一的区别是@include注入父变量,也可以发送其他变量 “收益率”和“包含率”之间有什么区别 我应该何时使用@yield 我应该何时使用@include @yield主要用于定义布局中的部分。当使用@extends扩展该布局时,您可以在视图中使用@section指令定义该部分中的内容 布局通常包含HTML、标题、正文、页眉和页脚。您在布局中定义了一个区域(@yield),扩展模板的页面将把内容放入该区域 在主模板中定义区

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

  • “收益率”和“包含率”之间有什么区别
  • 我应该何时使用@yield
  • 我应该何时使用@include

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

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

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

<body>
     @yield('content')
</body>
您在主页视图的“内容”部分的“内容”部分中定义的任何HTML都将被注入到该位置扩展的布局中

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

我强烈建议阅读Laravel网站上有关刀片模板的文档,以获得更全面的概述


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

@include-将单独文件的内容导入到当前文件的放置位置。i、 e:

布局文件:

< some html or other script >

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

< more html or other script >
< some html or other script >

@yield('needed_section_name')

< more html or other script >
@extends('layout.file_name')
... code as neeeded

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

...
more code as needed

@include('include.file_name')/“include.”表示文件所在的子目录
<更多html或其他脚本>
包含文件(带有代码块的刀片文件):


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

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

布局文件:

< some html or other script >

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

< more html or other script >
< some html or other script >

@yield('needed_section_name')

< more html or other script >
@extends('layout.file_name')
... code as neeeded

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

...
more code as needed

@收益率('所需部分名称')
<更多html或其他脚本>
设置为“扩展”该布局文件的“视图”刀片文件中需要以下部分

“查看”刀片文件:

< some html or other script >

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

< more html or other script >
< some html or other script >

@yield('needed_section_name')

< more html or other script >
@extends('layout.file_name')
... code as neeeded

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

...
more code as needed
@extends('layout.file\u name'))
... 按需要编码
@节(“所需的节名”)

@停止
...
根据需要提供更多代码
现在,布局文件将导入与使用的命名匹配的代码部分


关于这个主题的更多信息……

收益率和包含率之间的区别在于如何使用它们

如果你有一种静态的内容,比如导航栏,页面的这一部分将始终位于版面中的同一位置。在布局文件中使用
@include
时,导航栏将在每个布局中放置一次。但是如果您使用
@yield
,您将被迫在
@扩展布局的每个页面上创建导航栏的
@部分


另一方面,
@yield
是一个更好的选择,当所有页面上的内容都在变化,但您仍然希望在任何地方使用相同的布局时。如果您使用
@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.
@include('layouts.nav')//调用layout.blade.php时的默认值
@yield('content')//根据您的视图进行更改
@include('layouts.footer')//是的,如果您有特定的脚本,您可以使用@yield。

@当您的内容将被更改时,应使用yield
@include应用于不会更改的内容。e、 g页眉、页脚今天我也在试图找出这种区别,在哪里使用它们,以及为什么我要使用一个而不是另一个。请注意,这个答案是冗长的,可能解释得太多了

Laracasts论坛的斯内普让我开始正确地思考它们:

首先,@include将包含整个文件,就像PHP include函数一样。如果您只是将整个内容文件转储到页面的
,那就太好了,例如,下面将包含“content.blade.php”中的所有内容:


@包括('内容')

嘿,这是我的内容

诸如此类
但是@yield与@extends以及@section和@endsection指令一起,将允许您将内容分块到单独的部分,但将所有内容保存在一个文件中。然后,您可以@yield将其分块放入布局中。脑海中浮现的视觉效果是以经典的“即兴”洗牌方式将一副牌的一半洗牌到另一半:


@扩展('布局')
@节(“顶部内容”)
嘿,我是标题
@端部
@节(“中间内容”)
嘿,最近怎么样

@端部 @第节(“其他内容”) 现在结束了

@端部

@产量(‘最高含量’)
一些静态内容

@产量(‘中间含量’) 更多的静态内容

@收益率(“其他内容”) 某种静态页脚内容
其次也许更重要的是,控制流是反向的,以一种使一切更加连贯的方式。在第一个示例中,使用@include,您将使用