Hefeust CORTES
Hefeust CORTES

Reputation: 129

gulp + nunjucks couldn't render templates

I'm trying to use nunjucks in my JS stack.

I have pre-compiled my templates with gulp-nunjucks:

gulp.task("build:tpl", function() {
  var options = {
    // ... empty for the moment
  };

  return gulp.src("src/text/*.txt", { base : path.join(__dirname, "src/text") } )
    .pipe(nunjucks())
    .pipe(concat("ui-template.js"))
    .pipe(gulp.dest("js/tpl"));
});

Then I include ui-template with require JS for use:

/**
 * setup UI
 *
 * "ui-colorchess.txt" is the main template
 * it contains (nested) subtemplates (include "ui-history.txt" and so forth)
 */
UI.prototype.setup = function() {
  var that = this;

  // first we grab precompiled templates
  require(["js/tpl/ui-template.js"], function() {
    // ... into requirejs variables (or not)

    // some context vars, just to provide an ID in HTML
    var ctx = {
      id : "colorchess-1",
      otherStuff : 2
    };

    var env = new nunjucks.Environment(
      new nunjucks.WebLoader("/js/tpl")
    );

    // it sucks here !
    var c = env.render("ui-colorchess.txt", ctx);

    console.log(c);

     // try to assign result to a container
    that.bounds.innerHTML = c;

    // one more time... when it will work !
    // var board = new Board("colorchess-board-" + that.key);
    // board.firstDraw();
  });

And I get an error in my console:

Uncaught Template render error: (ui-colorchess.txt)
  TypeError: Cannot read property 'resolve' of undefined 

Can anyone identify what's wrong?

Upvotes: 1

Views: 2353

Answers (2)

Hefeust CORTES
Hefeust CORTES

Reputation: 129

I have solved the problem by doing the following:

  • renaming my templates in *.html
  • removing paths like "./subtemplate.txt" in my includes
  • setting configure("../html")
  • requiring correctily with requireJS

Upvotes: 0

xsh.7
xsh.7

Reputation: 6250

Same issue described in sindresorhus/gulp-nunjucks#1.

If you want to concatenate the files, you have to set the name option:

var options = {
  name: function (file) {
    return file.relative;
  }
};

Upvotes: 1

Related Questions