soflyy / breakdance-bugs

Bug reports from Breakdance users.
40 stars 6 forks source link

Custom Twig Function: Parameters not supported? #735

Open MrBushid0 opened 1 year ago

MrBushid0 commented 1 year ago

Hi,

Custom twig functions work great for simple functions but it looks like functions with parameters are not supported.

Can anyone confirm if this is by design and if so, would it be possible to add support for parameters as well?

Thanks

a-dubiel commented 1 year ago

looks like functions with parameters are not supported

Can you give me an example of this function?

MrBushid0 commented 1 year ago

looks like functions with parameters are not supported

Can you give me an example of this function?

Yeah, I mean like this.

function myTwigFunc() {
     return 'this will work';
}

\Breakdance\PluginsAPI\PluginsController::getInstance()->registerTwigFunction(
    'myTwigFunc',
    'MyPlugin\TwigFunctions\myTwigFunc',
    '() => { return "' . myTwigFunc() . '"; }',
    false
);

Then in a dependency or action calling {{myTwigFunction()}}, the above will return the specified string as expected.

This one below using a function with a parameter ' {{myTwigFunction('return me this string')}} ' won't work.

function myTwigFunc($prop) {
     return $prop;
}

\Breakdance\PluginsAPI\PluginsController::getInstance()->registerTwigFunction(
    'myTwigFunc',
    'MyPlugin\TwigFunctions\myTwigFunc',
    '(myProp) => { return "' . myTwigFunc(myProp) . '"; }',
    false
);

Maybe I'm just doing this wrong when trying to pass in a parameter to the function? I don't see any similar usage in the BD source code either, all the examples I saw are using a simple function with no parameters so I'm not sure if it's supported or not.

Thanks