树枝逃脱标签

时间:2011-10-27 17:04:40

标签: php twig

我有两个问题,一个是关于Twig代码语法中的标记名称,另一个是关于如何修改此标记。

标记:{{ 'Some text' }}{{ "Some text" }}

在两个示例中,它将显示“Some text”,但我想在显示之前使用我的函数(translate())。我不知道我应该在哪里搜索代码,因为我甚至不知道这个标签的名称。

我想在展示之前翻译解析后的文字,例如:{{ 'Some text' }} - &gt; <?php echo translate('Some text'); ?>

1 个答案:

答案 0 :(得分:0)

制作自己的Twig extenstion。教程可用here。这是最优雅的方式。

扩展名:

class Translate_Twig_Extension extends Twig_Extension
{    
    public function getFunctions()
    {
        return array(
            'translate' => \Twig_Function_Method($this, 'translate', array('is_safe' => array('html'))) 
        );
    }

    public function translate($text)
    {
        // do the magic, return translated text
    }
}

模板:

{{ translate('some text') }}