php Laravel @extends 和 @include
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39749683/
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
Laravel @extends and @include
提问by Kevin.a
I'm working on this project using Laravel.
我正在使用 Laravel 进行这个项目。
According to this tutorial I'm watching, I had to add this bit of code at the top of the main view.
根据我正在观看的本教程,我不得不在主视图的顶部添加这段代码。
@extends('layouts.masters.main')
Since I'm new to Laravel this got me wondering why can i not simply use.
由于我是 Laravel 的新手,这让我想知道为什么我不能简单地使用。
@include('layouts.masters.main')
I tried it instead and it did the same thing basically. Only thing is i do know how include works but i don't really know what extends does.
Is there a difference and so yeah what is it. Why did tutorial guy go for @extends
and not @include.
我尝试了它,它基本上做了同样的事情。唯一的问题是我知道 include 是如何工作的,但我真的不知道 extends 是什么。有什么区别吗,所以是什么。为什么辅导员会去@extends
而不是 去@include.
回答by Jonathon
@include
is just like a basic PHP include, it includes a "partial" view into your view.
@include
就像基本的 PHP 包含一样,它在您的视图中包含一个“部分”视图。
@extends
lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield
, which you can then put your own stuff into in your view file.
@extends
允许您“扩展”一个模板,该模板定义了自己的部分等。您可以扩展的模板将使用 定义自己的部分@yield
,然后您可以将自己的内容放入您的视图文件中。
Example:
例子:
template.blade.php
模板.blade.php
<html>
<body>
@yield('header')
@yield('content')
@yield('footer')
</body>
</html>
view-one.blade.php
视图one.blade.php
@extends('template')
@section('header')
View one's header
@endsection
@section('content')
View one's content
@endsection
@section('footer')
View one's footer
@endsection
Which will result in:
这将导致:
<html>
<body>
View one's header
View one's content
View one's footer
</body>
</html>
Now you could create another view which extends the same template, but provides its own sections.
现在您可以创建另一个扩展相同模板的视图,但提供自己的部分。
Another benefit to using @extend
is inheritance. You could provide a base template, and then another child template that extends that one which subsequently yields it's own sections. You can then extend that child template.
使用的另一个好处@extend
是继承。您可以提供一个基本模板,然后是另一个扩展该模板的子模板,随后生成它自己的部分。然后您可以扩展该子模板。
回答by JohanTux
@include
does not provide the structural skeleton that extending a layout template with @extend does. With @include
you're inserting a partial from another file.
@include
不提供使用@extend 扩展布局模板的结构骨架。随着@include
你从另一个文件插入一个部分。
extending a blade template provides a structure to the view that is defined in the layout template. For example, the layout:
扩展刀片模板为布局模板中定义的视图提供了结构。例如,布局:
layouts/layout.blade.php
布局/layout.blade.php
<!DOCTYPE html><html lang="en">
<head> <meta charset="UTF-8" /><title>DOCUMENT</title></head>
<body>
@yield('header')
@yield('content_1')
@yield('content_2')
@yield('content_3')
@yield('footer')
</body>
</html>
with the view
随着视图
show.blade.php
show.blade.php
@extends('layouts.layout')
@section('content_1')
<h2>Content1 Puppy Dog</h2>
@endsection
@section('footer')
<h1>I wanna be at the bottom</h1>
@endsection
@section ('content_3')
<h2>Content3 Horsie</h2>
@endsection
@section ('content_2')
<h2>Content2 Kitty Cat</h2>
@endsection
@section('header')
<h1>I wanna be at the top</h1>
@endsection
Gives the output:
给出输出:
<body>
<h1>I wanna be at the top</h1>
<h2>Content1 Puppy Dog</h2>
<h2>Content2 Kitty Cat</h2>
<h2>Content3 Horsie</h2>
<h1>I wanna be at the bottom</h1>
</body>
回答by Alexey Mezenin
@extends
with @section
is more powerful and convenient way to use layout(s), especially in larger apps. It allows to use inheritance. You can do the same with multiple @include
but it will be less readable and less maintainable solution.
@extends
with@section
是一种更强大、更方便的使用布局的方式,尤其是在较大的应用程序中。它允许使用继承。您可以对多个执行相同的操作,@include
但它的可读性和可维护性会降低。
Read more about @extends
and how it works here.
在此处阅读有关@extends
它及其工作原理的更多信息。