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.



7.8k

$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 mbsubstr 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) }}



4.1k

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!



4.1k
edited Mar '15

I do it with ob_* functions