Search code examples
javascripthtmljsongulpnunjucks

How to use content with HTML inside JSON data with nunjucks and gulp-data?


how to use HTML with JSON with Gulp-data ? I'm using it in a static website building process with Gulp, Nunjucks and MJML

JSON

{
 message:"Welcome to <span class='red'>world</span>"
}

.nunjucks file

{{ message }}

is giving this output in final HTML output

Welcome to &lt;span class=&#39;red&#39;&gt;world&lt;/span&gt;

Solution

  • Looks like Nunjucks uses autoescaping: true by default (due to their docs).

    gulp-nunjucks-render uses envOptions to configure template engine (line 20 of its code).

    You can try to pass autoescaping: false in options in gulp to disable escaping (more info in readme).

    Or you can just use {{ message | safe }} in your template to disable escaping for one piece of code. Mode info in API Docs: Autoescaping