Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Blade Directive With Arguments #1104

Open
imliam opened this issue Apr 7, 2018 · 1 comment
Open

[Proposal] Blade Directive With Arguments #1104

imliam opened this issue Apr 7, 2018 · 1 comment

Comments

@imliam
Copy link

imliam commented Apr 7, 2018

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.

@imbrish
Copy link

imbrish commented Apr 9, 2018

This won't work if you pass to your directives anything else than explicit values. @example($foo) will error because $foo is not defined when view is compiled.

I think the feature could be very useful, but it will require implementing some serious parsing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants