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

Insering dynamic variables into custom volt function doesn't work

Hello. I have a problem with accessing variables in my custom function.

So for example my code is:

{{ myFunction(variableFromController) }}

And PHP:

<?php

$compiler->addFunction('myFunction',function($resolvedArgs,$exprArgs)use($di,$compiler){
    $arg = $compiler->expression($exprArgs[0]['expr']);
    $lang = $di->get('lang');
    echo $lang->translate($arg);     
});

?>

So problem is that compiler will return variable as string "$variableFromController", where i'm doing it wrong ?



98.9k
edited Aug '14

Closures/Anonymous functions are not supposed to implement the functional code 'myFunction' must perform. Closures/Anonymous functions are supposed to return a string that is inserted into the PHP code generated by Volt:

$compiler->addFunction('myFunction',function($resolvedArgs, $exprArgs) use($compiler) {
    $arg = $compiler->expression($exprArgs[0]['expr']);
    return "\$this->lang->translate('" . $arg . "')";    
});


1.0k

Could you tell me your dev envirounment params? which OS and hardware? Cause I have problems with using custom functions at all.