Search code examples
nunjucks

How to escape imported partial in Nunjucks


I would like to escape the HTML code from an imported file in Nunjucks.

{% include "item.html" %}

The file "item.html" contains following code:

<strong>bold text</strong>

I would like the parent file (which includes item.html) to have the following output:

&lt;strong&gt;bold text&lt;/strong&gt;

I tried surrounding the include with an escape filter:

{% filter escape %}
  {% include "item.html" %}
{% endfilter %}

However in that case the file doesn't get included correctly. Any ideas?


Solution

  • I tested your snippet on Gulp + gulp-nunjucks-render@2.0.0, it does produce the desired effect:

    {% filter escape %}
    {% include "partials/test.html"  %}
    {% endfilter %}
    

    Output: filter escape works