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

旧时模样 提交于 2019-12-21 04:18:26

问题


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)


回答1:


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>


来源:https://stackoverflow.com/questions/14071428/how-can-i-set-up-a-user-defined-function-in-volt-phalcon

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!