Inline and Anonymous Components in Laravel Blade: A Practical Guide

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

Introduction

Laravel’s Blade template engine provides a powerful system for defining and using components within your applications. The introduction of inline and anonymous components with Laravel 7 made it even more flexible and streamlined for developers to create reusable chunks of UI markup and functionality without necessitating full component classes. In this comprehensive guide, we cover the ins and outs of using inline and anonymous components to build elegant and maintainable views in a Laravel application.

Understanding Blade Components

Before delving into inline and anonymous components, it’s important to understand what components are within the context of Blade. Blade components are reusable blocks of HTML and accompanying logic that can be utilized across different parts of your application. They resemble the concept of partials but are enhanced by the ability to receive data and handle logic.

Traditional components in Laravel consist of a class and a view. However, inline and anonymous components take this a step further by enabling developers to declare components directly within Blade files, without the need for a dedicated class – streamlining the process for simpler components.

Inline Components

Inline components allow you to define a component’s render method directly within the Blade file. This is particularly helpful for simple components that don’t require the overhead of a class.

Creating an Inline Component

<x-alert type="error" message="Something went wrong." />

To define how this component renders, you simply use @component directive:

@component('components.alert', ['type' => 'error', 'message' => 'Something went wrong.'])
<div class="alert alert-{{ $type }}">
    {{ $message }}
</div>
@endcomponent

You can define the component directly above the place where you’re using it if it’s a one-off or in a separate file for reuse.

Anonymous Components

Anonymous components are components without an associated class, defined only by their Blade files. These components are particularly useful when you need a bit more structure than provided by inline components but still want to avoid the overhead of a dedicated class.

Creating an Anonymous Component

To create an anonymous component, first, create a Blade file within the resources/views/components directory:

<!-- resources/views/components/warning.blade.php -->
<div class="alert alert-warning">
    {{ $slot }}
</div>

This file becomes your component. You can now use it in your Blade templates like so:

<x-warning>
    Attention: Please review your information.
</x-warning>

The {{ $slot }} directive is where the content you place between the component tags gets injected.

Passing Data to Components

Both inline and anonymous components can receive data. You can pass data to your components as attributes:

<x-profile-card :user="$user"/>

Inside the component, the provided data can be used just as if it were a variable passed to a regular Blade view:

<div class="profile-card">
    <h3>{{ $user->name }}</h3>
    <p>{{ $user->email }}</p>
</div>

Component Slots

Slots are placeholders within your components that can be filled with custom content each time you use the component:

<x-modal>
    <x-slot name="title">
        Confirmation
    </x-slot>

    Are you sure you want to proceed?

    <x-slot name="footer">
        <x-button />
    </x-slot>
</x-modal>

In the component view for modal, you’d define these slots:

<div class="modal">
    <div class="modal-header">{{ $title }}</div>
    <div class="modal-body">{{ $slot }}</div>
    <div class="modal-footer">{{ $footer }}</div>
</div>

Slot Defaults

You can provide default content for your slots, which will be rendered if no content is provided when using the component:

<!-- components/button.blade.php -->
<button>
    {{ $slot ?? 'Click me' }}
</button>

Conclusion

Inline and anonymous components simplify the component system significantly by allowing for rapid development without the need for separate classes when the logic is minimal. They promote a more sustainable and maintainable codebase by encouraging component reuse and modular design. However, for components that require complex logic or a significant amount of functionality, traditional class-based components may still be the best choice.

Thanks to Laravel’s elegant syntax and powerful features like inline and anonymous components, developers can create maintainable templates with ease. The pragmatic reasons for using such components are clear: ease of use, quick development, and a focus on what matters, writing the business logic for our applications.