Search code examples
node.jsgulpnunjucks

Array of multiple path is not working in Nunjucks for templates and partials


I'm using this gulp plugin to use nunjucks to make HTML management easier.

https://github.com/carlosl/gulp-nunjucks-render

gulp.task('default', function () {
  return gulp.src('src/templates/*.html')
    .pipe(nunjucksRender({
      path: ['src/templates/'] // String or Array
    }))
    .pipe(gulp.dest('dist'));
});

I want to keep my templates and partials in different folders so I tried this to keep as path

path: ['src/templates', 'src/partials']

but it's not working.

Template render error: (unknown path)
  Error: template not found: partials/side_nav.nunjucks

My setup screenshot from my editor


Solution

  • As far as I can see, the issue is in your include. You're already defining your base paths as 'src/templates' and 'src/partials'. Now nunchucks is trying to import src/templates/partials/side_nav.nunjucks and afterwards src/partials/partials/side_nav.nunjucks, which don't exist.

    Solution 1

    So you would have to include it without the partials part:

    {% include "side_nav.nunjucks" %}
    

    Solution 2

    If you want to be explicit about your folders (both templates and partials), you could just set you base path to src instead, and include your files like you did:

    {% extends "templates/layout_with_sidenav.nunjucks" %}
    ...
    {% include "partials/side_nav.nunjucks" %}