afbora / kirby-blade

Enable Laravel Blade Template Engine for Kirby 3
MIT License
20 stars 7 forks source link

Custom directives only receive one argument #19

Closed richstandbrook closed 2 years ago

richstandbrook commented 2 years ago

I've tried defining a custom directive in the config file, however the closure only ever receives one argument.

    'afbora.blade.directives' => [
        'test' => function ($foo, $bar) {
            return "bar:($bar) foo:($foo)";
        }
    ],

Usage:

@test('baz', 'quaz')

This results in the error:

ArgumentCountError thrown with message "Too few arguments to function Kirby\Filesystem\F::{closure}(), 1 passed and exactly 2 expected"

If I make the second argument optional function ($foo, $bar = null) then I get this output:

bar:() foo:('baz', 'quaz')

You see the second $bar properly is empty and $foo contains the string 'baz', 'quaz'

afbora commented 2 years ago

Actually, this is the working principle of blade directives. See same issue: https://stackoverflow.com/questions/41003733/pass-multiple-parameters-to-a-blade-directive

I know there is a problem with my directives, but you can use it like this:

@test(baz, quaz)

'afbora.blade.directives' => [
    'test' => function ($params) {
        list($foo, $bar) = explode(',', $params);
        return "bar:($bar) foo:($foo)";
    }
],
richstandbrook commented 2 years ago

Ahh I see yes. I had started to dig through the Blade code. Thanks, that makes sense