How to apply filters on Shopware sw_include (for example: |sw_sanitize)

264 Views Asked by At

Is there a way to use filters on the sw_include twig function?

This works:

{% sw_include '@Storefront/storefront/component/captcha/%s.html.twig'|format(capchaKey)...

I'd like to remove all tags from the include. Based on twig docu this should work:

{{ include('template.html')|striptags }}

However this does not throw any errors, but also does not apply the filter:

{% sw_include '@Storefront/storefront/component/delivery-information.html.twig'|sw_sanitize %}
1

There are 1 best solutions below

0
dneustadt On

The filters striptags and sw_sanitize work fairly different. striptags removes all html tags, but keeps their content. sw_santitize completely removes some specific tags that could potentially be harmful (like <script>) and removes them including their contents. This is why sw_sanitize may seem to you like it doesn't do anything, because it will keep "harmless" tags like <div>, <a> and some others intact.

When I tested it, the striptags filter actually didn't work applied to sw_include directly. You may want to use set to capture the contents of an include and then apply the filter on the variable.

{% set foo %}
    {% sw_include '@Storefront/storefront/component/delivery-information.html.twig' %}
{% endset %}
{{ foo|striptags }}