Why should we use Laravel blade ?

Viraj Madhushan
3 min readMar 28, 2022
Laravel Blade Template

The Blade is a powerful, tempting engine in a Laravel framework. The blade allows using the tempting engine easily, and it makes the syntax writing very simple. The blade tempting engine provides its own structure, such as conditional statements and loops. To create a blade template, you just need to create a view file and save it with a “.blade.php” extension instead of .php extension.

The blade templates are mainly stored in the “/resources/” directory. The main advantage of using the blade template is that we can create the master template, which can be extended by other files.

So In this article, let’s see about top reasons to why should we use Laravel blade snippets and how easy they are.

🌟 Easy to display dynamic data

Handling data in Laravel is very easy and highly effective. If you want to print the value of a variable, then you can do so by simply enclosing the variable within the curly brackets.

{{$variable}}

🌟 DRY — (Don’t Repeat Yourself)

dry

“Don’t repeat yourself” (DRY) is a principle of software development aimed at reducing repetition of software patterns, replacing it with abstractions or using data normalization to avoid redundancy.

Laravel allow you to easily create layouts, extend them and include partials to prevent you repeating the same HTML in multiple files.

Defining A Blade Layout

Defining blade layout may be little familiar for everyone. But you may be unfamiliar with that @ mark. That's how blade codes write with HTML. So, because of defining blade layout, we do not repeat that HTML tags inside this layout. We can reuse this template.

<!--This is main layout template-->
<html>
<head>
<title>App Name - @yield('title')</title> </head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>

Using A Blade Layout

@extends('layouts.master')@section('title', 'Page Title')@section('sidebar')@@parent
<p>This is appended to the master sidebar.</p>
@stop@section('content')
<p>This is my body content.</p>
@stop

This extends and sections are comes handy with Laravel 9 as components. With new Laravel 9 we can create x-components and reuse them. That makes Laravel application more clean and robust.

<x-custome-alert/>

🌟 Debugging is easier than normal PHP file

Laravel generally use object-oriented PHP. As every OOP language, this one also easy to debugging. Generally we can implement layout, components and views separately. That helps to making debugging easier in Laravel blade.

Think about the below example. This code write for each loop to display some data without blade. 👇

foreach($a as $a){
echo ‘<td>’.$a->data.’</td>’;
}
//Or<?php foreach($a as $a): ?>
<td><?= $a->data ?></td>
<?php endforeach; ?>

And this is how Laravel blade use it.👇

//with blade
@foreach
<td>{{ $a->data }}</td>
@endforeach

What is less difficult to debug? Definitely with blade snippets are easier to understand.

Conclusion

The Blade engine basically allows you to do six things:

  • Define sections
  • Extend views
  • Echo and escaping
  • Ifs
  • Loops
  • Includes

    In a cleaner and a shorter way than normal PHP, and it also allows you to easily create layouts, extend them and include partials to prevent you repeating the same HTML in multiple files. Laravel blade templates Just look better, and easier to maintain. So that’s why we should use Laravel blade templates.

--

--

Viraj Madhushan

Passionate learner of AI and Robotics. Always exploring new technologies to stay ahead of emerging trends. Hiking and reading enthusiast.