Laravel Blade Template

In this tutorial you will learn about the Laravel Blade Template and its application with practical example.

Blade is the in built templating engine for Laravel framework.Blade is a very powerful and easy to use templating engine that makes writing syntax very easy and readable. Blade templating engine comes with its own control structure such as conditional statements and loops. It is very easy to create a Blade template, simply create your view file and save it with the .blade.php extension instead of .php, blade templates are typically stored in the resources/views directory. The main advantage of using blade template engine is that it allow use to create master template which can be extended by other individual pages.

Echoing data

If you want to display any variable inside blade view, you can do it be simply wrapping the variable in “curly” braces.

Syntax:-

Above syntax is equivalent to <?= $variable ?> in plain PHP

Ternary Operator

Blade provide a short-cut equivalent to ternary operator in PHP

Syntax:-

Above syntax is equivalent to <?= isset($variable) ? $variable: ‘Default Value’ ?>

Blade Template Control Statements

The Blade template engine comes with convenient short-cuts for using PHP control statements.

Blade If Statements

Blade template engine provides the @if, @elseif, @else and @endif directives to construct equivalent PHP if statement and its counterparts.

Example:-

Blade also provides an @unless directive as a conditional statement.

Example:-

Blade also provides an @hasSection directive to determine if a given section has any content or not.

Example:-

Blade Loops

Blade template engine provides @for, @endfor, @foreach, @endforeach, @while and @endwhile directives to construct equivalent PHP loop statements .

Including other views

Blade template engine provides @include(‘viewname’) directive for including a view inside another view. The child view will have all the variables that are available to parent view.

Example:-

Defining a Master layout

As we know most of the web applications follows the same layout across all the pages, so the better approach is to define a master template where we can place all the boilerplate.In Laravel, blade template engine allow us to define a master template which can be extended by other individual pages.

Example:-

Step 1:- Create a new folder “layout” in /resources/views/ directory.

Step 2:- Create a new file “master.blade.php” in /resources/views/layouts/ directory.

Step 3:- Copy the following code in the “master.blade.php” file we created.

Here, in the above master template –

@yield(‘title’) is used to display the value of the title
@section(‘sidebar’) is used to define a section named sidebar
@show is used to display the contents of a section
@yield(‘content’) is used to display the contents of content

Extending Master Layout

We will now show you how to extend the master layout we just created.

Step 1:- Create a new view file page.blade.php in /resources/views/
Step 2:- Copy the following code in page.blade.php file

Here, in the above page –

@extends(‘layouts.master’) extends the master layout
@section(‘title’, ‘Page Title’) sets the value of the title section.
@section(‘sidebar’) defines a sidebar section in the child page of master layout
@parent displays the content of the sidebar section, defined in the master layout.
<p>This is appended to the master sidebar.</p> adds paragraph content to the sidebar section
@endsection ends the sidebar section
@section(‘content’) defines the content section
@endsection ends the content section

Step 3:– Open app/Http/routes.php and configure the route as below –

Step 4:- Now open the following URL in the browser to see the output.

http://localhost:8000/page

 

In this tutorial we have learn about the Laravel Blade Template and its application with practical example. I hope you will like this tutorial.