Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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
});


1.2k
Accepted
answer

It works! Thanks a lot.