Laravel @extends和@include [英] Laravel @extends and @include

查看:44
本文介绍了Laravel @extends和@include的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel进行这个项目.

I'm working on this project using 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')

由于我是Laravel的新手,这让我想知道为什么我不能简单地使用它.

Since I'm new to Laravel this got me wondering why can i not simply use.

   @include('layouts.masters.main')

我尝试了一下,它基本上做了同样的事情.唯一的事情是我确实知道include的工作原理,但是我真的不知道extends会做什么. 有区别吗,是的,这是什么.为什么辅导员选择@extends而不选择@include.

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就像基本的PHP包含一样,它在您的视图中包括一个部分"视图.

@include is just like a basic PHP include, it includes a "partial" view into your view.

@extends允许您扩展"模板,该模板定义了自己的部分等.您可以扩展的模板将使用@yield定义自己的部分,然后您可以将自己的内容放入视图文件中

@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.

示例:

template.blade.php

<html>
    <body>
        @yield('header')
        @yield('content')
        @yield('footer')
    </body>
</html>

view-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

这将导致:

<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.

使用@extend的另一个好处是继承.您可以提供一个基础模板,然后提供另一个扩展该子模板的子模板,该子模板随后将产生其自己的部分.然后,您可以扩展该子模板.

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.

这篇关于Laravel @extends和@include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆