This repository was archived by the owner on Jul 16, 2021. It is now read-only.

Description
When you define a Blade::directive(…) your closure usually gets back a plain expression as a string. However, in a lot of cases I find myself wanting to do one or two things to the arguments in the directive definition - often just small things for the sake of presentation that don’t warrant their own helper function too.
Imagine that BladeCompiler was Macroable and we could do this to accept a new callback and extract the arguments first.
Blade::macro('directiveWithArgs', function ($name, callable $handler) {
Blade::directive($name, function($expression) use ($handler) {
$args = eval("return [{$expression}];");
return $handler(...$args);
});
});
This then lets us define and use new Blade directives with the arguments we want the directive to accept directly, just like you’d normally define a function.
Blade::directiveWithArgs('example', function($a, $b, $c = 'give', $d = 'you') {
return "<?php echo '$a $b $c $d up'; ?>";
});
<p>
@example('Never', 'gonna')
</p>
I feel like this drastically lowers the cognitive overload when reading a directive and it doesn’t immediately pass the inner expression off to another function and would love to see it as an option in the future.