I submitted a PR to bump illuminate/support to 5.5 which includes the new @switch statements and my personal favorite, custom if statements.
The custom if statements work similar to the directives that I posted examples of above, but instead can simplify if statements dramatically.
A small example would be checking if an ACF field exists. Before, our directive would look somewhat like this:
$sage = sage('blade')->compiler();
/**
* Create @hasField() Blade directive
*/
$sage->directive('hasField', function ($expression) {
$expression = strtr($expression, ['(' => '', ')' => '']);
return "<?php if (Acf::field($expression)->get()) : ?>";
});
/**
* Create @endField Blade directive
*/
$sage->directive('endField', function () {
return "<?php endif; ?>";
});
Not too bad, but a little dramatic to make two separate directives, and if you’re anything like me, to achieve a clean Blade syntax I was ending up with dozens of Blade directives that were simply an endif.
With the new Custom If Statements, that shouldn’t be such an issue anymore. To achieve the above, we simply do:
$sage = sage('blade')->compiler();
/**
* Create @hasField() Blade directive
*/
$sage->if('hasField', function ($field) {
return Acf::field($field)->get();
});
and this alone will automatically create an @endHasField() directive allowing you to quickly check if your field exists.
To clarify, ->if() will always expect a boolean.
[note: above examples are using acf-fluent.]
Edit: ->if() might have potential issues with Sage at the moment. See here