Blade is the templating engine provided by Laravel. It allows the creation of master templates that can be extended by child views/pages. The main benefits are template inheritance and sections. Views use the .blade.php extension and can contain plain PHP code. Variables are output using {{ }}, and it provides directives like @if and @for to construct PHP control structures. Blade templates can include other views using @include and define common elements in a master layout to reduce duplication across pages.
3. 3
Blade is the simple, yet powerful templating engine
provided with Laravel. Blade view files use the .blade.php
file extension and are typically stored in the
resources/views directory.
4. 4
It does not restrict the
developer from using
plain PHP code in views.
The blade views thus
designed, are compiled
and cached until they are
modified.
5. 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.
5
6. Two of the primary benefits of using Blade are template
inheritance and sections. We can define a blade page as
a combination of layout and sections.
6
8. If you want to display any variable inside blade view, you can do it
be simply wrapping the variable in “curly” braces. By default,
Blade {{ }} statements are automatically sent through PHP's
htmlspecialchars function to prevent XSS attacks.
Above syntax is equivalent to <?= $variable ?> in plain PHP
8
9. The Blade template engine comes with convenient short-
cuts for using PHP control statements. Blade template
engine provides the @if, @elseif, @else and @endif
directives to construct equivalent PHP if statement and
its counterparts.
9
10. 10
Blade template engine provides @for, @endfor,
@foreach, @endforeach, @while and @endwhile
directives to construct equivalent PHP loop
statements.
11. 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.
11
12. 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.
12
13. 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.
13
15. • @yield(‘title’) is used to display the value of the title
• @section(‘sidebar’) is used to define a section named
sidebar
• @endsection is used to end a section named sidebar
• @show is used to display the contents of a section
• @yield(‘content’) is used to display the contents of
content
15
16. 16
We will now show you how to extend the
previously 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
18. • @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.
18
19. • @endsection ends the sidebar section
• @section(‘content’) defines the content section
• @endsection ends the content section
19
20. Now we can create a simple foundation for the front-end views
for our site. Layouts, slices, and pages all work together to
create an easy templating system. There is much more that
Laravel Blade Templating and I encourage you to take a look at
what else we can do
20
21. Andi Nurkholis, S.Kom., M.Kom.
September 28, 2020November 02, 2020
Study Program of Information Systems
Faculty of Engineering and Computer Science
SY. 2020-2021