aalaap
aalaap

Reputation: 4411

How can I capture the content wrapped in a pair of custom Blade directives?

I am trying to write a custom Blade directive which can modify the content and return it, something like this:

<div class="some-text">
    @uppercase
    This is a line of text.
    @enduppercase
</div>

which would render in the HTML as following:

<div class="some-text">
    THIS IS A LINE OF TEXT.     
</div>

What I do NOT wish to know is how to do the following:

@uppercase('This is a line of text')

How can I capture all the content within the start and end directives, process and then return them to the view?

Note: There is a similar sounding question here, with a comment linking to another question that purportedly has the answer, but it doesn't really answer the question I have described here.

Upvotes: 2

Views: 624

Answers (1)

aalaap
aalaap

Reputation: 4411

I found the answer shortly after posting this question.

Add the following to your AppServiceProvider::boot() method:

\Blade::directive('uppercase', function () {
    return '<?php ob_start(); ?>';
});

\Blade::directive('enduppercase', function () {
    return '<?php echo strtoupper(ob_get_clean()); ?>';
});

I referred to this code for the idea: https://github.com/RobinRadic/blade-extensions/blob/master/src/Directives/EndspacelessDirective.php

Upvotes: 2

Related Questions