Useful Blade Directives

Posted 3 years ago by tylernathanreed

What Blade Directives do you find yourself adding? Most of my projects end up having a BladeServiceProvider, and here's some quite useful directives that I add:

@continue Directive for Loops:

// Add @continue for Loops
Blade::directive('continue', function($expression)
    return '<?php continue; ?>';

@break Directive for Loops:

// Add @break for Loops
Blade::directive('break', function($expression)
    return '<?php break; ?>';

@ifempty Directive for Loops:

This is handy because sometimes you don't actually need the @forelse directive, and only the @empty part.

// Add @ifempty for Loops
Blade::directive('ifempty', function($expression)
    return "<?php if(count$expression == 0): ?>";

// Add @endifempty for Loops
Blade::directive('endifempty', function($expression)
    return '<?php endif; ?>';

@optional Directive for Sections:

I often find myself wanting to have a section, but only use it if it's yielded.

// Add @optional for Complex Yielding
Blade::directive('optional', function($expression)
    return "<?php if(trim(\$__env->yieldContent{$expression})): ?>";

// Add @endoptional for Complex Yielding
Blade::directive('endoptional', function($expression)
    return "<?php endif; ?>";

Here's an example of where I use @optional:


    <section id="overlay" class="overlay hide">
        <div class="overlay-wrapper">
            <div id="overlay-content" class="overlay-content">
            <button id="overlay-close" class="overlay-close" onclick="overlay.close()">Close</button>


        <li id="overlay-open"><a href="javascript:void(0);" onClick="">Open Overlay</a></li>



        <script src="/media/js/partials/overlay.min.js"></script>



So, if the overlay section is used, I make sure to have a container for it, an "Open Overlay" button on the footer of the site to re-open the overlay, and the overlay.min.js file used to automatically display the overlay, and control its visibility.

If the section isn't used, then none of this HTML appears, which is great for decluttering a ton of empty containers within my templates.

The last directive that I have is @set, because I end up being so happy using Blade everywhere, that I just don't want to use <?php $var = (Some Value); ?>. Therefore I made the @set directive:

// Add @set for Variable Assignment
Blade::directive('set', function($expression)
    // Strip Open and Close Parenthesis
    if(Str::startsWith($expression, '('))
        $expression = substr($expression, 1, -1);

    // Break the Expression into Pieces
    $segments = explode(',', $expression, 2);

    // Return the Conversion
    return "<?php " . $segments[0] . " = " . $segments[1] . "; ?>";

What directives do you guys add?

Side Note: @JeffreyWay is there a way to escape the @ formatting? The Blade Directives are being listed as people, even in the Code Blocks.

Double Side Note: This isn't a conversation that would ever have an answer. Having the ability to say "No answer required", or "I'm not looking for an answer", etc. would be nice.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.