我正在学习 Laravel(从版本 5.3 开始),这两个 Blade 指令看起来非常相似,唯一的区别是,@include
可以注入父级变量并且可以发送其他变量。
@yield
和 @include
有什么区别?
何时应该使用 @yield
?
何时应该使用 @include
?
我正在学习 Laravel(从版本 5.3 开始),这两个 Blade 指令看起来非常相似,唯一的区别是,@include
可以注入父级变量并且可以发送其他变量。
@yield
和 @include
有什么区别?
何时应该使用 @yield
?
何时应该使用 @include
?
@yield
主要用于在布局中定义一个部分。当使用@extends
扩展布局时,您可以使用@section
指令在视图中定义该部分的内容。
布局通常包含您的HTML、<head>
、<body>
、<header>
和<footer>
。您在布局中定义一个区域(@yield
),用来容纳继承该模板的页面的内容。
在您的主模板中定义该区域。例如:
<body>
@yield('content')
</body>
假设你的主页扩展了该布局
@extends('layouts.app')
@section('content')
// home page content here
@endsection
在主页视图中定义的任何 HTML 内容都将注入到扩展布局中相应的位置,并显示在“内容”部分。
@include
类似于标准的 PHP include ,用于可重用的 HTML。它不像 @yield
和 @section
那样具有父/子关系。@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 >
包含文件(一个包含代码块的 Blade 文件):
< some cool code here >
'file_name' 的内容(也是一个 blade 文件)会在 @include 指令所在的位置被导入。
@yield 从子文件(视图 blade 文件)中的 "section" 导入代码,例如:
布局文件:
< some html or other script >
@yield('needed_section_name')
< more html or other script >
需要在“view”刀模板文件中添加以下部分,该文件已经被设置为“扩展”该布局文件。
“View”刀模板文件:
@extends('layout.file_name')
... code as neeeded
@section('needed_section_name')
< some cool code here >
@stop
...
more code as needed
现在布局文件将在与所使用命名相匹配的代码部分中导入。
更多相关内容请参见此处....
@yield
和@include
的区别在于使用方式。@include
时,导航栏将每个布局放一次。但是,如果您使用@yield
,则必须在每个@extends
布局的页面上制作一个@section
的导航栏。@yield
是在所有页面上更改内容但仍希望在各处使用相同布局的更好选择。如果使用@include
,则必须为每个页面创建新布局,因为内容不同。今天我也在试图弄清楚它们之间的差异,以及何时使用每个,为什么要使用其中一个而不是另一个。请注意,本答案可能会很啰嗦并且解释得太多。
Laracasts论坛的Snapey让我开始认真思考它们:
https://laracasts.com/discuss/channels/laravel/whats-the-difference-between-atinclude-and-atyield
首先,@include将包含整个文件,就像PHP include函数一样。如果您只是将整个内容文件转储到页面的<body>
中,那么这非常棒,例如以下内容将包含'content.blade.php'内的所有内容:
<!-- layout.blade.php -->
<body>
@include('content')
</body>
<!-- content.blade.php -->
<div>
<div>
<p>Hey this is my content.</p>
</div>
<div>
<span>and stuff</span>
</div>
</div>
<!-- content.blade.php -->
@extends('layout')
@section('top_content')
<h1>Hey I'm the title</h1>
@endsection
@section('middle_content')
<p>Hey how's it going</p>
@endsection
@section('other_content')
<p>It's over now.</p>
@endsection
<!-- layout.blade.php -->
<body>
<div>
@yield('top_content')
</div>
<p>Some static content</p>
<div>
@yield('middle_content')
</div>
<p>Some more static content</p>
<div>
@yield('other_content')
</div>
<div>Static footer content of some kind</div>
</body>
Route::get('/', function () {
return view('layout');
});
Route::get('/', function () {
return view('content');
});
Route::get('/welcome', function () {
return view('layout', ['content' => 'welcome']);
});
Route::get('/profile', function () {
return view('layout', ['content' => 'profile']);
});
<!-- layout.blade.php -->
<body>
@include($content)
</body>
我觉得那看起来很混乱。
话虽如此,@include 似乎是在你的布局文件(由 @extends 指令调用的文件)中包含一小段代码的好方法,比如导航栏、页脚或者其他你想为了组织目的而分离出来的内容。
@include 用于可重复使用的代码,如导航栏,我们只需要设计一次导航栏,就可以在整个网站中使用它。
@yield 用于像主体这样会不断变化的部分。
当您的内容将被更改时,应使用@yield。对于不会更改的内容(例如页眉和页脚),应使用@include。
layout.blade.php
中进行yield。如果每个页面都有特定的脚本或样式,您也可以使用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.
@yield
和@include
都将HTML注入到父文件中。Laravel文档也非常差。它不针对新手使用laravel的人群。@yield
和@include
的主要区别是:@include
定义视图注入到父级,而@yield
定义要注入的section。只有当你的视图@extends
父视图时,@yield
才会起作用。 - user1651105