简体   繁体   中英

How can I set up a user defined function in Volt (Phalcon)

How can I set up a user defined function in Volt? For instance I want to call a function that would translate strings in my views as such:

<div class='page-header'>
    <h2>{{ tr('session_login_title') }}</h2>
</div>

and I want the tr to map to a function \\My\\Locale::translate($key)

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM