2. What is template engine?
• Used to code “View” web application pages.
• Template engines are used when you want to
rapidly build web applications that are split into
different components.
• Templates also enable fast rendering of the server-
side data that needs to be passed to the application.
• For example, you might want to have components
such as body, navigation, footer, dashboard, etc.
3. Blade: Laravel template engine
Code is
rendered
to PHP in
the storage
folder
Original
View
pages
4. Advantages of Laravel Blade Template
The Blade is a powerful templating engine in a Laravel framework that
provides:
• Clean code (easy to use and readable).
• Separation of business logic and UI.
• Inheritance.
• Easy for Non-PHP front end developers.
• Provide many built in features such as Auto escaping.
• Auto-escaping via avoiding Cross-Site Request Forgery (CSRF)
✓ CSRF is an attack that forces an end user to execute unwanted actions
on a web application in which they’re currently authenticated.
✓ An attacker may trick the users by sending a link via email or chat and
end with executing actions of the attacker’s choosing.
✓ A successful CSRF attack can force the user to perform state changing
requests like transferring funds, changing their email address..etc.
5. Blade Features
• The blade templating engine provides its own structure such
as conditional statements and loops.
• To create a blade template, create a view file and save it
with a .blade.php extension instead of .php extension.
• The blade templates are stored in the /resources/view.
• The blade templates are rendered to PHP
/storage/framework/view directory.
• The main advantage: creating a master template, which can
be extended by other files.
• You can write plain PHP code in PHP syntax.
• You can write plain PHP code in template syntax.
6. Declare variable
• Mostly variables are values of input types, however, we can
define a variable for simple testing purposes.
• Syntax
• The above syntax is equivalent to<?php $id=1; ?>.
• Note: the variable declaration will not be used in the
following slides.
@php
$id =1
@endphp
7. Displaying Data
• If you want to print the value of a variable, then you can do
so by simply enclosing the variable within the curly brackets.
• Syntax
• In blade template, we do not need to write the code
between
• The above syntax is equivalent to<?= $variable ?>.
{{$variable}};
<?php echo $variable; ?>
8. Displaying Data
• Ternary operator
• In blade template, the syntax of ternary operator can be
written as:
• If the variable is not set for a value, a ‘default value’ string is
printed.
• Note that if the variable is not defined it will show an
‘undefined variable’ error message.
• The above syntax is equivalent to
{{{ $variable or 'default value’}}}
<?= isset($variable) ? $variable : ?default value? ?>
9. Blade Template Control Statements
1.<html>
2.<body>
3. <font size='5' face='Arial'>
4.@if(($id)==1)
5.student id is equal to 1.
6.@else
7.student id is not equal to 1
8.@endif
9.</font>
10.</body>
11.</html>
Blade Code
Output
10. Blade Template Control Statements
1.<html>
2. <body>
3. <font size='5' face='Arial'>
4.@unless($id==1)
5.student id is not equal to 1.
6.@endunless
7.</font>
8.</body>
9.</html>
Blade Code
Output
Blade interally changes
unless(condition) into
if(! condition)
11.
12. For Loop in Blade
• The blade templating engine provides loops such as
• @for, @endfor
• @foreach, @endforeach
• @while, and @endwhile directives. These directives are
used to create the php loop equivalent statements.
@for ($i = 0; $i < 10; $i++)
{{$i}}
@endfor
13. For each Loop in Blade
• Loop through array and skips displaying an item if not set to
value.
1.@foreach($students as $students)
2.{{{$students}}}
3.@endforeach