我正在尝试在我的JS堆栈中使用双节棍。
我已经用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"));
});然后,我使用require包含ui-template:
/**
* 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 有人能找出哪里出了问题吗?
发布于 2015-04-17 00:57:00
与sindresorhus/gulp-nunjucks#1中描述的问题相同。
如果你想连接这些文件,你必须设置name选项:
var options = {
name: function (file) {
return file.relative;
}
};发布于 2015-04-17 01:30:56
我通过执行以下操作解决了这个问题:
在我的includes
https://stackoverflow.com/questions/29676755
复制相似问题