Get In Touch
Diamond Marg, Kupondole heights ,
Lalitpur-10 ,Nepal,
dev@ux-qode.com
Ph: +977 980-1039816
Work Inquiries
dev@ux-qode.com
Ph: +977 980-1039816
Back

Creating Custom Directive on Laravel Blade Template

What is Blade?

Blade is the templating engine used in the Laravel PHP framework. It provides a convenient and expressive way to create dynamic views in your web applications. Blade templates allow you to separate the presentation logic from the actual PHP code, making your codebase more organized and maintainable.

Blade templates provide a clean and elegant way to create dynamic and maintainable views in Laravel applications, making it easier to manage your frontend code and presentation logic separately from your backend code.

Laravel Blade Directives:

Blade directives are special syntax elements in Laravel’s Blade templating engine that allow you to embed PHP code and logic within your templates while maintaining a more readable and concise format. They provide a way to execute common tasks, control structures, and other dynamic operations directly within your view files. Laravel comes with several built-in Blade directives, and you can also create your own custom directives to extend the templating engine’s capabilities.

Common Blade Directives:

  1. @if, @else, @endif: These directives are used for conditional statements. For example:

@if($condition)

    // Code to display if condition is true

@else

    // Code to display if condition is false

@endif

  1. @foreach, @endforeach: Used for iterating over arrays or collections:

@foreach($items as $item)

    // Code to display for each item

@endforeach

  1. @for, @endfor: Loop through a specified number of iterations:

@for($i = 0; $i < 5; $i++)

    // Code to display for each iteration

@endfor

  1. @php, @endphp: Used for writing php codes in blade

@php

    // PHP code here

@endphp

Creating Custom Blade Directives:

You can create your own custom Blade directives to encapsulate specific functionality that you find yourself using frequently in your views. To do this, you need to define a directive in a service provider class. Here’s how you can create a custom directive:

  1. Create a Service Provider: First, create a new service provider using the Artisan command:

php artisan make:provider CustomBladeDirectivesServiceProvider

  1. Register the Directive: Open the CustomBladeDirectivesServiceProvider.php file created in the app/Providers directory. In the boot method, define your custom directive using the Blade facade:

use Illuminate\Support\Facades\Blade;

public function boot()

{

    Blade::directive(‘mydirective’, function ($expression) {

        // Your directive logic here

        return “<?php // Output generated by my directive ?>”;

    });

}

  1. Use the Custom Directive: You can now use your custom directive in your Blade templates:

@mydirective($someVariable)

Remember that the $expression parameter in the closure of the directive represents the content passed to the directive in the template. You can parse and manipulate this content as needed within your directive logic.

After creating the custom directive, don’t forget to register your service provider in the config/app.php configuration file in the providers array:

‘providers’ => [

    // …

    App\Providers\CustomBladeDirectivesServiceProvider::class,

],

With custom Blade directives, you can encapsulate complex or repetitive tasks into reusable code blocks, improving the clarity and maintainability of your Blade templates.

Nirjal Prasad Wagle
Nirjal Prasad Wagle
https://uxqode.co/

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This website stores cookies on your computer. Cookie Policy