我试图在我的JS堆栈中使用nunjucks。
我已经使用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-template与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
任何人都可以确定错误吗?
答案 0 :(得分:1)
sindresorhus/gulp-nunjucks#1中描述的相同问题。
如果要连接文件,则必须设置name
选项:
var options = {
name: function (file) {
return file.relative;
}
};
答案 1 :(得分:0)
我通过执行以下操作解决了问题: