Search code examples
javascriptnode.jsincludegruntjsnunjucks

Include in nunjucks (grunt-nunjucks-2-html) are based on an incorrect path


I'm using Nunjucks with grunt/node via the grunt plugin grunt-nunjucks-2-html

My root path is where the gruntfile is, so it's look like this:

./src
    index.html
    expo.html
    ./inc
        head.html
        header.html

My gruntfile config looks like this :

nunjucks: {
    render: {
        files: [
            {
                expand: true,
                cwd: 'src/',
                src: "*.html",
                dest: pathbuild,
                ext: ".html"
            }
        ]
    }
},

in my index.html I have this:

{% include "inc/head.html" %}

When I try grunt nunjucks, this is what I get

Warning: (unknown path) Error: template not found: inc/head.html
Use --force to continue.

That can be solved if I change path to "src/inc/head/html" but I don't get why I need to specified this, seems not logical to me.

Do you have something to teach to me that I've missed so hard? Thanks.


Solution

  • I had the same issue and after looking at the plugin code, I noticed that the template path had to be supplied as an array:

    nunjucks: {
      options: {
        paths: ['templates'], // 'templates' as a string can now be passed (30Oct2014)
        data: grunt.file.readJSON('results.json')
      },
      render: {
        files: [
           {
              expand: true,
              cwd: "templates/",
              src: "*.html",
              dest: "build/",
              ext: ".html"
           }
        ]
      }
    }
    

    I've sent a pull request (https://github.com/vitkarpov/grunt-nunjucks-2-html/pull/4) so if it gets accepted, we'll be able to supply the template path as a string.

    Update 30-Oct-14: The pull request has been merged, so a string or array can be passed to options.paths.