Javascript 狼吞虎咽+;nunjucks不能';t渲染模板

Javascript 狼吞虎咽+;nunjucks不能';t渲染模板,javascript,gulp,nunjucks,Javascript,Gulp,Nunjucks,我正在尝试在JS堆栈中使用NUNJUCK 我已经用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()) .p

我正在尝试在JS堆栈中使用NUNJUCK

我已经用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"));
});
然后,我将ui模板包括在require JS中以供使用:

/**
 * 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();
  });
我的控制台出现了一个错误:

Uncaught Template render error: (ui-colorchess.txt)
  TypeError: Cannot read property 'resolve' of undefined 
有人能识别出问题出在哪里吗?

与中描述的问题相同

如果要连接文件,必须设置
名称
选项:

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

我已通过以下操作解决了此问题:

  • 在*.html中重命名我的模板
  • 删除my includes中类似“/subtemplate.txt”的路径
  • 设置配置(“../html”)
  • 用requireJS正确地要求