Why should we use Laravel blade ?

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.

--

--

--

I’m a person who passionate to learn about new 🤖 technologies, gain more 💻 programming knowledge & ✈️ travel through the world.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Apache Spark 3.0— What’s new ?

Unity In-World Text

Rancher 2 snippets: best practices, backup, Prometheus…

Reverse Vowel of a String.

The Pinternship: A Retrospective

Solana extension wallet user guide — MathWallet

CDP Part 1: Evolution of Lenskart’s Compute and Development Platform (CDP)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Viraj Madhushan

Viraj Madhushan

I’m a person who passionate to learn about new 🤖 technologies, gain more 💻 programming knowledge & ✈️ travel through the world.

More from Medium

How to add custom properties to Laravel Activities Log

Could Not Open Input File: Artisan In Laravel

Laravel 9 API CRUD with Authentication

Laravel 9 API CRUD with Authentication

Customising Laravel Fortify 2FA methods