如何在 Nunjucks 中转义导入的部分

How to escape imported partial in Nunjucks

我想从 Nunjucks 中导入的文件中转义 HTML 代码。

{% include "item.html" %}

文件 "item.html" 包含以下代码:

<strong>bold text</strong>

我希望父文件(包括 item.html)具有以下输出:

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

我尝试用转义过滤器包围包含:

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

但是在那种情况下,文件没有被正确包含。有什么想法吗?

我在 Gulp + gulp-nunjucks-render@2.0.0 上测试了你的代码片段,它 产生了预期的效果:

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

输出: