18

I want to use Nunjucks templates but want to pass in my own JSON data to be used on the templates.

The documentation here is pretty sparse.

https://mozilla.github.io/nunjucks/templating.html

Thank you.

sjmartin
  • 3,912
  • 4
  • 19
  • 32
  • See [Passing data to includes](https://github.com/mozilla/nunjucks/issues/539#issuecomment-203538566) from the Nunjucks Issues – KyleMit Aug 31 '19 at 23:41

4 Answers4

11

You can use gulp-data which allows you to pass json files to the task runner you're using to render Nunjucks.

gulp.task('nunjucks', function() {
  return gulp.src('app/pages/**/*.+(html|nunjucks)')
    // Adding data to Nunjucks
    .pipe(data(function() {
      return require('./app/data.json')
    }))
    .pipe(nunjucksRender({
      path: ['app/templates']
    }))
    .pipe(gulp.dest('app'))
});
Jędrzej Chałubek
  • 1,410
  • 1
  • 16
  • 29
yahyazini
  • 700
  • 12
  • 23
  • Do you know if there is a webpack relevant for gulp-data? I want to pass data.json to nunjucks with Webpack. – levipadre Feb 26 '20 at 20:59
7

Simply use dump and safe filters combined:

<script>
  const choices = {{ yourJsonVar | dump | safe }};
</script>
brydar
  • 151
  • 1
  • 4
2

Found this gem here in 2019, but thought it'll be helpful for people like me, who have searched but didn't find anything.

First, create a template part with expected arguments in a macro

{% macro render(secTitle, secSubtitle) %}
    <h4 class="heading">
        {{ secTitle | safe }}, {{ secSubtitle | safe }}
    </h4>
{% endmacro %}

Save it as [yourname].tpl - in this case heading.tpl

Then, import this block and use a function specified in macro above (render() in this case)

{% block heading %}
    {% import "components/heading.tpl" as heading with context %}
    {{ heading.render(
        secTitle='Hello there',
        secSubtitle='General Kenobi'
    ) }}
{% endblock %}

This will result in:

<h4 class="heading">
    Hello there, General Kenobi
</h4>

Please notice | safe after strings in a component - this means it'll output the string with HTML formatting (eg. <br> will create a new line instead of outputting <br> in text).

3binco
  • 21
  • 1
  • 2
    The only thing you are missing in this example is how you associate json data from somefile.js to your template macro. I think you would get more upvotes on revealing that. – klewis Feb 27 '21 at 14:47
0

You can use their async render to achieve that.

https://mozilla.github.io/nunjucks/api.html#render

$.getJSON('/path/to/file.json', function (result) {
        nunjucks.render('path/to/template/file.html', { result : result }, function (err, res) {
            // do something
        });
    });
Mubashar Aftab
  • 183
  • 2
  • 9
  • This seems to just print `[object Object]` for me. – suryanaga Apr 08 '16 at 14:59
  • That's because you are getting an array containing the objects inside it. You need to extend the result : result to how your JSON is constructed like result : result[0] or pass the first array item in the function's parameter. – Mubashar Aftab Feb 20 '17 at 11:11