Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

php - Laravel @extends and @include

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')

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

Example:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...