Hello Laravel Friends,

I am here with yet another important subject. Today it’s about the Blade Template in Laravel.

The blade is a powerful templating engine that is included with Laravel. Blade template files use the .blade.php file extension and are typically stored in the resources/views directory. To create Blade Template, you just need to create a view file with .blade.php extension instead of .php extension.

Blade helps us to create a master template that other files can extend, and this is its main advantage.

Displaying Variable

You may display a variable that is passed to your Blade views by wrapping the variable in curly braces.

For example, given the following route:

You may display the contents of the name variable like the following:

Blade & JavaScript Frameworks

Javascript frameworks also use “curly” braces to indicate a given expression should be displayed in the browser, you use @ symbol to inform the Blade rendering engine never to touch.

Blade Directives

Blade templates also provide some standard PHP control structures, such as conditional statements and loops. These shortcuts provide an immaculate, terse way of working with PHP control structures while remaining familiar with their PHP counterparts.

If Statements

You may construct if statements using the @if, @elseif, @else, and @endif directives. These directives function identically to their PHP counterparts:

Output: 

if statements

You may also use the @unless directive to reverse of if statement.

For example:

Loops

In addition to conditional statements, Blade provides simple directives for working with PHP’s loop structures. Each of these directives functions identically to its PHP counterparts. When using loops, you may also end the loop or skip the loop using the @continue and @break directives. It works the same as the “continue” and “break” of the PHP common loop.

Foreach Loop

  • Controller File 
  • Blade File
Output: 

blade file

For Loop

You can use the for loop of common php structure using @for, @endfor Blade directives.

Output: 

for loop

Switch Statements

In the Blade template, you can use the switch statements using the @switch, @case, @break, @default, and @endswitch directives.

Example – 

Including Subviews

In some cases, you may need to include some other view file. You can archive using the @include directive.

Example – 

Create a header.blade.php file that includes that file in your layout.

Conclusion

This was all about the Blade template in Laravel. Learn How to Create Blade Template Layout in Laravel.

If you have any doubts, share them with me through the comment section. Share the article with your friends and stay in touch with us to learn more about the Laravel framework.

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]