Volt custom filters - evaluate variables

How can I get variable value in custom filter?

// volt template
{{ text|own }}

// filter definition
 $volt->getCompiler()->addFilter('own', function($resolvedArgs, $exprArgs) {

The variable $resolvedArgs does not contain evaluated value of {{ text }} but only string '$text'. How can I get real value to filter it after?

Thank you.


A custom Volt filter is intended to generate PHP code that will be executed later by the PHP, when Volt is compiling the template, it does not know where the template will be executed and which values are going to be passed to the custom filters and functions, what you need to do is create a helper class that will receive the real value when PHP executes the compiled template:


class MyHelper
    public static function own($data)
        return $transformedData;

Then you can generate the PHP code that calls this helper:

$volt->getCompiler()->addFilter('own', function($resolvedArgs, $exprArgs) {
    return 'MyHelper::own(' . $resolvedArgs . ')';


This is a bit complicated but seems working.

Is it possible to use somehow DI in helper?

class MyHelper extends \Phalcon\Tag { ...}

I supposed that self::getDI() should works but I can't get any service (registered before volt service)?

Is there also any trick how to debug this part? As it just don't render anything if there is error but var_dump + die is not working...(I tried ob_flush() etc. but wasn't helpful]


It seems that var_dump + die is working but only if there is really no error - otherwise no output.

Still I didn't figured out how to access to DI or send it through parameters as they probably have to be STRINGs only.

Thank you very much!


does the job.