2013-07-17 79 views
0

我嘗試在Symfony2中發送的HTML代碼:方法返回HTML Symfony2的

public function testAction() 
{ 
    $html = '<input type="text">' 
    return $this->render('TestBundle:Default:index.htlm.twig',array(
    'html' => $html, 
)); 
} 

當我使用的HTML變量在頁面index.html.twig這樣的:{{ html }}結果是

<input type="text"> 

但我不想要這個結果。我想要的結果是頁面index.html.twig顯示我的輸入(文本區域)。

回答

1

從DOC的答案:http://twig.sensiolabs.org/doc/api.html#escaper-extension

的逃避者擴展增加自動輸出轉義來嫩枝。它定義了一個標籤,autoescape和一個過濾器raw。

在創建逃避者擴展,可以開啓或關閉全球輸出轉義的策略:

$escaper = new Twig_Extension_Escaper('html'); 
$twig->addExtension($escaper); 

如果設置爲HTML,模板中所有的變量都逃脫(使用HTML逃逸策略),除使用原始過濾器:

{{ article.to_html|raw }} 

您也可以通過使用autoescape標籤(見小枝1.8之前使用的語法autoescape DOC)改變當地的逃逸模式:

{% autoescape 'html' %} 
    {{ var }} 
    {{ var|raw }}  {# var won't be escaped #} 
    {{ var|escape }} {# var won't be double-escaped #} 
{% endautoescape %}