We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Problem with using function as argument in predefined macro

Hi.

I have a problem with using my functions in Volt. Ok, i have a simple function:

$volt->getCompiler()->addFunction('icon', function($resolved_args, $expr_args) {
    return '\Helpers\VoltFunctionsHelper::icon(' . $resolved_args . ');';
});
public static function icon($key, $classes = '') {
    return '<i class="fa fa-' . trim($key) . ($classes !== '' ? ' ' . trim($classes) : '') . '"></i>';
}

I would like to use the result of my function as an argument for the macro "link_to", like I do bellow:

{{ link_to('somelink', image('imagelink')) }}
{{ link_to('somelink', icon('someicon')) }}

My function "icon" works correctly when loosely in the template code, but when I try to use it in macro "link_to" as link text, I get an parse error:

Parse error: syntax error, unexpected ';', expecting ']'

Why am I getting this error? How could I define a macro function instead (not in template source)?

Replace this:

$volt->getCompiler()->addFunction('icon', function($resolved_args, $expr_args) {
    return '\Helpers\VoltFunctionsHelper::icon(' . $resolved_args . ');';
});

With this:

$volt->getCompiler()->addFunction('icon', function($resolved_args, $expr_args) {
    return '\Helpers\VoltFunctionsHelper::icon(' . $resolved_args . ')'; // <-- REMOVED the semicolon ; here
});


3.5k
Accepted
answer

It works! Thanks a lot.