Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel @extends and @include

Tags:

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.

like image 580
Kevin.a Avatar asked Sep 28 '16 13:09

Kevin.a


2 Answers

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

like image 147
Jonathon Avatar answered Sep 19 '22 23:09

Jonathon


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

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

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

@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>
like image 20
JohanTux Avatar answered Sep 22 '22 23:09

JohanTux