Volt functions act as string replacements and do not actually call the underlying function. Volt translates the function into the relevant string which in return is interpreted by PHP.
Suppose you have a Locale
class which has a translate
method as such:
public static function translate()
{
$return = '';
if (isset(self::$_phrases[$key]))
{
$return = self::$_phrases[$key];
}
return $return;
}
This method uses the $_phrases
internal array to find the relevant key that you pass and return the text of the phrase you want. If not found it returns an empty string.
Now we need to register the function in Volt.
$di->set(
'volt',
function($view, $di) use($config)
{
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(
array(
'compiledPath' => $config->app_volt->path,
'compiledExtension' => $config->app_volt->extension,
'compiledSeparator' => $config->app_volt->separator,
'stat' => (bool) $config->app_volt->stat,
)
);
$volt->getCompiler()->addFunction(
'tr',
function($key)
{
return "\\My\\Locale::translate({$key})";
}
);
return $volt;
},
true
);
Note how the tr
function is registered. It returns a string \My\Locale::translate({$key})
with the passed $key
parameter. This Volt syntax will be translated to PHP directives and executed by PHP. Therefore the view string:
<div class='page-header'>
<h2>{{ tr('session_login_title') }}</h2>
</div>
after Volt processes it becomes:
<div class='page-header'>
<h2><?php echo \My\Locale::translate('session_login_title') ?></h2>
</div>