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 ?



83.4k
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 . "')";    
});

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