twig 的 html_entity_decode (opencart)

mpi*_*elz 4 opencart twig

我试图在我的产品页面(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 %}
Run Code Online (Sandbox Code Playgroud)

但是技术细节字段中存储了无样式的列表..这会输出完整的 html 而不是渲染列表。

我尝试过使用{{ attribute.text|e }}and{{ attribute.text|raw }}以及我能找到的许多其他替代方案..但每次都只是抛出 html 而不是渲染它..

在 php 中这曾经有效。

<?php echo html_entity_decode($attribute['text']); ?>
Run Code Online (Sandbox Code Playgroud)

那么我现在如何解码 html,因为我不能在 twig 中使用 php 并且 twig 中也没有 html_entity_decode :(

期待一些帮助:)

非常感激

谢谢。

Dar*_*Bee 8

只需将html_entity_decode函数注册到twig. 最简单的方法是查看twig加载位置并添加以下代码,

$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');
Run Code Online (Sandbox Code Playgroud)

之后,您可以在twig模板中执行以下操作

{{ attribute.text|html_entity_decode }}
Run Code Online (Sandbox Code Playgroud)

更新:对于 Opencart 3.0.3.7 版本过滤器应该是这样的:

$twig->addFilter(new \Twig\TwigFilter('html_entity_decode','html_entity_decode'));
Run Code Online (Sandbox Code Playgroud)