Directives in Laravel Blade: Tutorial & Examples

Updated: January 15, 2024 By: Guest Contributor Post a comment

Introduction

Laravel Blade is a powerful templating engine provided with Laravel, which allows PHP code to be easily managed within HTML. Blade templates offer various directives which give you convenient shortcuts for common PHP functionalities. This tutorial will introduce you to Blade directives with a range of simple to advanced examples.

Understanding Blade Directives

Directives in Blade are essentially pre-defined keywords that trigger specific PHP behavior when included in Blade templates. Directives begin with @ symbol followed by the directive name. Here are some of the most commonly used blade directives:

  1. @section and @yield for defining sections and displaying content.
  2. @extends for inheriting from a master layout.
  3. @include for including partial views.
  4. @if, @elseif, @else, and @unless for conditional statements.
  5. @for, @foreach, @while, and @forelse for loops.
  6. @php to inline PHP code.
  7. Custom directives which can be defined using the directive method of the Blade facade.

Basic Directives

Control Structures

Control structures are used to manage the flow of information in Blade templates. Here are examples of conditional and looping directives:

<!-- if Statement -->
@if($condition)
   <p>Condition is true.</p>
@endif

<!-- if-else Statement -->
@if($condition)
   <p>Condition is true.</p>
@else
   <p>Condition is false.</p>
@endif

<!-- unless Statement -->
@unless($condition)
   <p>Condition is false.</p>
@endunless

<!-- foreach Loop -->
@foreach($array as $element)
   <li>{{ $element }}</li>
@endforeach

<!-- forelse Loop -->
@forelse($array as $element)
   <li>{{ $element }}</li>
@empty
   <p>No items found.</p>
@endforelse

<!-- while Loop -->
@while($condition)
   <p>I'm looping</p>
   @php $condition = false @endphp
@endwhile

Layouts and Includes

Extending a Layout

Using the @extends directive, views can inherit from a master layout.

<!-- master.blade.php -->
<!DOCTYPE html>
<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>

<!-- home.blade.php -->
@extends('master')

@section('title', 'Home Page')

@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

Including Sub-views

With the @include directive, you can include a view within another view.

<div>
    @include('shared.errors')

    <form>
        <!-- Form Contents -->
    </form>
</div>

Advanced Directives

Creating Custom Directives

Laravel allows you to define custom directives for additional functionality. This can be done within the boot method of a service provider.

use Blade;

// Inside a service provider boot method:
Blade::directive('datetime', function ($expression) {
    return "<?php echo ($expression)->format('m/d/Y H:i'); ?>";
});

// Usage in Blade template:
@datetime($dateVariable)

Environment Detection

You can use the @production and @env directives to change how the application behaves based on the environment.

@production
    <script src="https://cdn.example.com/jquery.min.js"></script>
@else
    <script src="/js/jquery.js"></script>
@endproduction

@env('staging')
    <!-- special staging environment configurations -->
@else
    <!-- configurations for other environments -->
@endenv

Stacks

Using Stacks for Assets

Blade allows you to define named stacks which can be pushed to and rendered elsewhere in the layout.

@push('scripts')
    <script>console.log('Pushed to the script stack.');</script>
@endpush

// Later in the layout:
@stack('scripts')

Conclusion

This tutorial covered the basics and some advanced usage of Laravel Blade’s directives. By using these directives effectively, you can keep your views clean, modular, and maintainable. Keep in mind that a powerful application is not just about how it runs — it’s also about how it’s written.