0

How can I include data to gulp-nunjucks template from separate file?

//template/data/data.html

{% set 
list = [
    {
        title: 'Item1'
    },
    {
        title: 'Item2'
    }
] 
%}

This simple solution doesn't work.

{% include "data/json.html" %}
Skif
  • 1,178
  • 4
  • 14
  • 26

1 Answers1

0

This should work if you use import instead of include, https://mozilla.github.io/nunjucks/templating.html#import

Try this (I used .njk extensions, but you can use .html, it won't matter):

//template/data/data.njk

{% set list = [
  {
    title: 'Item1'
  },
  {
    title: 'Item2'
  }] %}

And in the file where you want to use the {{ list }} variable:

//template/other-file.njk

{% from 'data/data.njk' import list %}
{{ list }}

Any top-level variables that are {% set %} or any macros that are defined are available via import.

Kevin Powell
  • 596
  • 5
  • 7