html_entity_decode for twig (opencart)

泄露秘密 提交于 2019-12-11 03:09:53

问题


im trying to output an attribute of a product on my product page (opencart v3).

The attribute is called 'technicaldetails' and it works just fine using this code:

{% 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 %}

but the technical details field have unstyled list stored in it.. and this outputs the complete html instead of rendering the list.

ive tried using {{ attribute.text|e }} and {{ attribute.text|raw }} and many other alternatives i could find.. but each time is just throws out the html and not render it..

in php this used to work.

<?php echo html_entity_decode($attribute['text']); ?>

so how can i decode the html now as i cant use php in twig and there is no html_entity_decode in twig either :(

looking forward for somehelp :)

much appreciated

thanks.


回答1:


Just register the html_entity_decode function in twig. The most simple way is to look where twig is loaded and add the following code,

$twig->addFilter(new \Twig_Simple_Filter, 'html_entity_decode', 'html_entity_decode');

After that you can just do the following in your twig templates

{{ attribute.text|html_entity_decode }}



回答2:


Find file

document_root/system/library/template/twig.php

Just after

$this->twig = new \Twig_Environment($loader, $config);

add following code

$twig->addFilter(new \Twig_SimpleFilter('html_entity_decode', 'html_entity_decode'));

After doing this, you must went to admin to reload all modifications in menu Extensions -> modifications. After that you can do the following in all twig files *.twig

{{ attribute.text|html_entity_decode }}


来源:https://stackoverflow.com/questions/45985449/html-entity-decode-for-twig-opencart

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!