, ,

What will you learn from Blade Template in Laravel?

Posted by

Guys, In this chapter, we’re going to look at the basics of the Blade Template which plays a prominent role in Laravel. so, what following topics we will discuss regarding blade templates
like what is blade templates, how to display data, what about blade directives, and how to create layouts through blade templates & convert templates into layouts.

Laravel Blade Template

What is a blade Template?

Let’s move to the path of blade templates. so, Blade templates are their own template engines of Laravel and this template engine of blade has its own structure such as conditional statements and loops. if you want to view something on your page or echo then they provide their own syntax like conditional and looping statements whatever have you seen in php core, it gives the alternatives or replacement of all those statements that also be easy to implement.

Before what were we doing so, we were making the file by the name of .php extension but in this we make the file by the name of .blade.php extension so, .blade.php what it describes, this gives the information to Laravel that it is a blade template file.

Displaying Data

Suppose, you have to display data so, what you had to do in core PHP. you had to follow the syntax like

PHP Core Syntax:

<?php

echo $name;

?>

Blade Template Syntax:

{{$name}}

{!!$name!!}

if $name is a variable to that someone’s name is assigned but what do you do in Laravel, you have to apply blade template syntax which is mentioned above like
{{$name}} -It doesn’t decode the Html entities, it means suppose you have made a string in that you put the h1 tag with a closing tag So, it will print as usual the whole string.
{!!$name!!} -It decodes the whole Html entities, suppose you have a string in that you put an h1 tag with a closing tag then it will not print the whole string. it simply decodes the
data that you have put inside the Html tag.

Blade Directives

Let’s talk about the blade directives, what sorts of directives, we’re using at displaying data such as

Blade conditional directives. It includes

  • @if, @elseif, @else, and @endif directives. for example:-

Input:

If and Else Directive

Output:

Conditional Directive
  • @unless, @endunless directives. for example:

Input:

Unless and Endunless Directive

Output:

Unless Directive
  • @isset, @endisset directives. for example:

Input:

Isset Directive

Output:

Isset and Endisset Directive

Blade Looping directives: It includes

  • @for and @endfor directives. for example:

Input:

For Directive

Output:

For and Endfor Directive
  • @while and @endwhile directives. for example:
While Directive

Output:

While and Endwhile Directive
  • @foreach and @endforeach directives. for example:

Input:

Foreach Directive
Foreach and Endforeach Directive
  • @break and @continue directives. for example:

Input:

Break Directive

Output:

Break and Continue Directive

Other Blade directives. It includes

  • If you want to put comments on Blade templates of Laravel then you apply this for

comments: {{–Comments goes here–}}

  • For including: You may apply @include on your blade templates for including another page.
  • For raw PHP: @php @endphp

Creating Layouts in Blade Template

Blade Template Layout

Let’s understand the layouts in Blade templates, the things that come under the layout blade directives are:

  • @yield directive: it is used to display the contents of a given section
  • @section directives: it defines a section of data or content.
  • @extends blade directives: which particular view, do you want to ‘inherit’? it just works in favor of that.

so, let’s understand the two main things are:

  • @stack directive: if you want to render the stack data or contents so, you will pass the name of the stack to the @stack directive.
  • @push and @endpush: it basically used to push content or data into the stack.
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x