【发布时间】:2017-08-31 15:44:58
【问题描述】:
我试图在我的产品页面(opencart v3)上输出产品的属性。
该属性称为“technicaldetails”,使用以下代码可以正常工作:
{% if attribute_groups %}
{% for attribute_group in attribute_groups %}
{% if attribute_group.name == 'technicaldetails' %}
{% for attribute in attribute_group.attribute %}
{{ attribute.text }}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
但技术细节字段中存储了无样式列表。这会输出完整的 html 而不是呈现列表。
我尝试使用 {{ attribute.text|e }} 和 {{ attribute.text|raw }} 以及我能找到的许多其他替代方案。但每次都只是抛出 html 而不是渲染它..
在 php 中这曾经可以工作。
<?php echo html_entity_decode($attribute['text']); ?>
所以我现在如何解码 html,因为我不能在 twig 中使用 php 并且在 twig 中也没有 html_entity_decode :(
期待一些帮助:)
非常感谢
谢谢。
【问题讨论】:
-
attribute.text 以 html 格式存储在 DB 中。它被编码了,这就是我使用
html_entity_decode()函数并且以前可以工作的原因。但是现在 opencart 使用 twig 而我没有知道解决这个问题的方法:( -
它是存储为原始 HTML 还是编码的 HTML?就像当您直接查看数据库时,标签存储为
&lt;&gt;或&lt;&gt;?如果答案是后者,您可能需要创建一个htmlspecialchars_decode自定义 Twig 过滤器 -
这是它在数据库中的样子(直接从那里复制)
<ul class='technicaldetails'> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> -
使用
|raw应该可以工作,除非 OpenCart 正在对您使用的任何内容进行额外的转义。或者,出于安全原因,他们不允许您尝试做的事情?尝试制作一个 Twig 过滤器并使用它 -
|raw给出了同样的结果。|e也是如此