2011-10-27 113 views
0

我有兩個問題,一個是關於Twig代碼語法中的標籤名稱,另一個是關於如何修改此標籤。樹枝逃生標籤

標籤:{{ 'Some text' }}{{ "Some text" }}

在這兩個例子中,將顯示「一些文本」,但我想顯示器前,用我的功能(translate())。我不知道我應該在代碼中搜索,因爲我甚至不知道這個標籤的名稱。

我想顯示器前解析的文本翻譯,例如:{{ 'Some text' }} - ><?php echo translate('Some text'); ?>

回答

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') }}