我希望有人能夠幫助我解決這個問題。Twig轉義HTML並渲染爲原始
我從數據庫中提取細節以顯示在樹枝模板上(使用Symfony2),但將其保存在數據庫中的方式使得難以解釋HTML。
基本上,HTML標籤已經被翻譯成表中的實體,例如:
<p>Bach Flower Crab Apple Remedy:&nbsp;the "cleansing" Remedy can be used both internally and externally&nbsp;</p><p><strong>
等。我已經研究了樹枝的渲染選項,並試圖(渲染產品說明的環基於我)以下:
{% set strategy = 'html' %}
{% autoescape 'html' %}
{{ product.description|escape('html')|raw }}
{% endautoescape %}
,也只是:
{{ product.description|raw }}
第一種方法只是呼應了現有的內容(實體),第二種方法只是呈現HTML標籤的頁面,如下所示:
<p>Bach Flower Crab Apple Remedy: the "cleansing" Remedy can be used both internally and externally.</p><p><strong>...
所以,你可以看到,我不能找到一種方法,實際上解釋HTML標籤爲了顯示描述應該是。
有沒有辦法做到這一點?我不能做到這一點在PHP所有它做的是發送對象到通過循環模板:
public function showAction(Request $request, $store_id=0)
{
$max = 1000;
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
$products = $repository->getProductsByStoreId($store_id,$max);
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate(
$products,
$request->query->get('page', 1),
20
);
$return['products'] = $pagination;
$return['categories'] = $this->getCategories();
return $this->render('AppBundle:tables:productstable.html.twig', $return);
}
因此,您已經在數據庫中轉義了HTML,並且您想將其作爲原始HTML輸出爲HTML格式?爲什麼它首先在數據庫中轉義? *這是你應該糾正的核心問題。 – deceze
如果可能的話,不要在將數據存入數據庫時對數據進行編碼。一個好的閱讀將是[@ deceze的「大逃亡」](http://kunststube.net/escapism/)。 Deceze!你不能等待幾秒鐘;) – Yoshi
@Yoshi我似乎被吸引到這些類型的問題...:d – deceze