We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Volt extending

Can I create new type of block? Something like functions, but with another syntax. For example (for translations):

{% trans %}Some content{% endtrans %}

A new function (like «{{ trans("") }}») — is not a very good method for me.


$view->registerEngines(array( '.phtml' => function($view, $di) use ($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array( 'compiledPath' => $config->application->cacheDir, 'compiledSeparator' => '_' )); $compiler = $volt->getCompiler(); //add custom function truncate likes as mb_substr into volt $compiler->addFunction('truncate', function($val, $pass) use($compiler) { $value = $compiler->expression($pass[0]['expr']); $start = 0; $end = strlen($value); if (isset($pass[1])) $start = $compiler->expression($pass[1]['expr']); if (isset($pass[2])) $end = $compiler->expression($pass[2]['expr']); return 'mb_substr('.$value.','.$start.','.$end.',"utf-8")'; }); return $volt; } )); return $view;

you can use it in volt for example : {{ truncate($variable.field,0,20) }}


SliceOfLife did you found the solution? I need some own tags with this syntax {% tag %}Some content{% endtag %}

I would love to see a solution for this, because I need something like this right now!

edited Mar '15

I do it with ob_* functions