Laravel Blade @prepend Directive

Laravel Blade @prepend Directive

In version 5.4 Laravel added a new components and slots feature that simplifies building HTML elements into reusable areas.

As an example of how this features works pretend you have a layout.blade.php file that includes this:

<ul id="sidebar">
    @stack('sidebar')
</ul>

Now when you want to insert data into this section from a sub-view you can add:

@push('sidebar')
    <li>Sidebar list item</li>
@endpush

Once this is rendered in the browser the results are an unordered list:

<ul id="sidebar">
    <li>Sidebar list item</li>
</ul>

Now since v5.4.10 a new prepend directive has been which allows you to push an item to the front of the stack before it’s rendered.

For example:

@push('sidebar')
    <li>Sidebar list item</li>
@endpush

@prepend('sidebar')
    <li>First Sidebar Item</li>
@endprepend

Now, the results will be:

<ul id="sidebar">
    <li>First Sidebar Item</li>
    <li>Sidebar list item</li>
</ul>

This will be great in those situations where you need to push items to the front of the stack.


Filed in: Laravel 5.4 / Blade


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Lead Front End Developer
Remote
continued
Full-Stack Laravel Developer
Cologne
Revive Interior
Intermediate PHP Developer (Full Stack | CakePHP | Laravel | Vue | jQuery)
Remote
continued
Senior PHP Developer (Full Stack | CakePHP | Laravel | Vue | jQuery)
Remote
continued
Laravel Developer
Amsterdam (partially remote possible)
Let's Book
Web Developer (Laravel)
Tweed Heads, New South Wales, AUSTRALIA
Tursa Employment & Training
Laravel Experts needed-Remote position
Remote
Golden Sky ROI