我正在学习Laravel(从5.3版本开始),这两个看起来非常相似,我知道唯一的区别是@include注入父变量,也可以发送其他变量.
Rob*_*eca 25
@yield主要用于定义布局中的一个部分.当使用@extends扩展该布局时,您可以在视图中使用@section指令定义该部分中的内容.
布局通常包含HTML,头部,正文,页眉和页脚.您可以在布局中定义一个区域(@yield),扩展模板的页面将放置其内容.
在主模板中,您可以定义区域.例如:
<body>
@yield('content')
</body>
Run Code Online (Sandbox Code Playgroud)
让我们说您的主页扩展了该布局
@extends('layouts.app')
@section('content')
// home page content here
@endsection
Run Code Online (Sandbox Code Playgroud)
您在"内容"部分的主页视图的内容部分中定义的任何HTML都将被注入到该点中扩展的布局中.
@include用于可重用的HTML,就像标准的PHP包含一样.它没有像@yield和@section这样的父/子关系.
我强烈建议您阅读Laravel网站上的Blade Templates文档,以获得更全面的概述
https://laravel.com/docs/5.0/templates
G-M*_*Man 18
@include和@yield是将代码导入当前文件的两种完全不同的操作类型.
@include - 将单独文件的内容导入到放置它的位置的当前文件中.即:
布局文件:
< some html or other script >
@include('include.file_name') // "include." indicates the subdirectory that the file is in
< more html or other script >
Run Code Online (Sandbox Code Playgroud)
包含文件(带有代码块的刀片文件):
< some cool code here >
Run Code Online (Sandbox Code Playgroud)
然后在@include指令所在的位置导入'file_name'(也是刀片文件)的内容.
@yield从子文件中的"section"导入代码("视图"刀片文件.)即:
布局文件:
< some html or other script >
@yield('needed_section_name')
< more html or other script >
Run Code Online (Sandbox Code Playgroud)
"视图"刀片文件中需要以下部分,该文件设置为"扩展"该布局文件.
"查看"刀片文件:
@extends('layout.file_name')
... code as neeeded
@section('needed_section_name')
< some cool code here >
@stop
...
more code as needed
Run Code Online (Sandbox Code Playgroud)
现在,布局文件将在与所使用的命名匹配的代码部分中导入.
更多关于这个问题在这里 ....
@yield和之间的区别在于@include您如何使用它们。
如果您有静态内容,例如导航栏,则页面的这一部分将始终位于布局中的同一位置。当您@include在布局文件中使用时,导航栏将在每个布局中放置一次。但是,如果您使用,@yield您将被迫在布局的@section每个页面上制作导航栏@extends。
@yield另一方面,当所有页面上的内容都发生变化但您仍然希望在任何地方使用相同的布局时,这是一个更好的选择。如果您使用,@include您将不得不为每个页面制作一个新布局,因为内容不同。
| 归档时间: |
|
| 查看次数: |
19418 次 |
| 最近记录: |