Skip to content

Template inheritance

Jorge Castro edited this page Jan 10, 2021 · 11 revisions

Template Inheritance

In the master page (layout)

Tag Note
@section('sidebar') Start a new section
@show Indicates where the content of the section will be displayed
@yield('title') Show here the content of a section

Using the master page (using the layout)

Tag Note
@extends('layouts.master') Indicates the layout to use
@section('title', 'Page Title') Sends a single text to a section
@section('sidebar') Start a block of code to send to a section
@endsection End a block of code

Note :(*) This feature is in the original documentation but it's not implemented either is it required. Maybe it's an obsolete feature.

Sub Views

Tag Note
@include('folder.template') Include a template
@include('folder.template',['some' => 'data']) Include a template with new variables
@includeif('folder.template') Include a template only if it exists
@includefast('folder.template') Include a template (at compile time)
@each('view.name', $array, 'variable') Includes a template for each element of the array
Note: Templates called folder.template is equals to folder/template

@include

It includes a new template inside our template.

Example: /examples/testinclude.php

You could include a template as follow:

<div>
    @include('shared.errors')
    <form>
        <!-- Form Contents -->
    </form>
</div>

You could also pass parameters to the template

@include('view.name', ['some' => 'data'])

Scope of the arguments of @include

By default, every argument used in a include is kept globally. For example:

@include("template",['a1'=>'hello']) <!-- a1 is equals to hello -->
@include("template",['b1'=>'world']) <!-- a1 is equals to hello, b1 is equals to world-->
<!-- a1 is equals to hello, b1 is equals to world-->

However, it is possible to limit the life of the arguments by setting the field $includeScope as true.

$blade=new BladeOne();
$blade->includeScope=true;
@include("template",['a1'=>'hello']) <!-- a1 is equals to hello -->
@include("template",['b1'=>'world']) <!-- a1 is not defined, b1 is equals to world-->
<!-- both a1 and b1 are not defined -->

You can see an example of it in /examples/testinclude_scope.php

@includeif

Additionally, if the template doesn't exist then it will fail. You could avoid it by using includeif

@includeIf('view.name', ['some' => 'data'])

@includefast

@Includefast is similar to @include. However, it doesn't allow parameters because it merges the template in a big file (instead of relying on different files), so it must be fast at runtime by using more space on the hard disk versus less call to read a file.

@includefast('view.name')

This template runs at compile time, so it doesn't work with runtime features such as @if() @includefast() @endif()

aliasing include

Laravel's blade allows to create aliasing include. Laravel calls this method "include()". However, PHP 5.x doesn't allow to use the name "include()" so in this library is called "addInclude()".

How it work?

If your BladeOne includes are stored in a sub-directory, you may wish to alias them for easier access. For example, imagine a BladeOne include that is stored at views/includes/input.blade.php with the following content:

📁 views/includes/input.blade.php

<input type="{{ $type ?? 'text' }}">

You may use the include method to alias the include from includes.input to input.

Blade->addInclude('includes.input', 'input');

Once the include has been aliased, you may render it using the alias name as the Blade directive:

@input(['type' => 'email'])
Clone this wiki locally