我正在尝试在我的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
谁能确定出什么问题了?
最佳答案
sindresorhus/gulp-nunjucks#1中描述的相同问题。
如果要串联文件,则必须设置name
选项:
var options = {
name: function (file) {
return file.relative;
}
};
关于javascript - gulp + nunjucks无法呈现模板,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29676755/