首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gulp + nunjucks无法呈现模板

gulp + nunjucks无法呈现模板
EN

Stack Overflow用户
提问于 2015-04-16 21:48:44
回答 2查看 2K关注 0票数 1

我正在尝试在我的JS堆栈中使用双节棍。

我已经用gulp nunjucks预编译了我的模板:

代码语言:javascript
复制
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:

代码语言:javascript
复制
/**
 * 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();
  });

在我的控制台中我得到了一个错误:

代码语言:javascript
复制
Uncaught Template render error: (ui-colorchess.txt)
  TypeError: Cannot read property 'resolve' of undefined 

有人能找出哪里出了问题吗?

EN

回答 2

Stack Overflow用户

发布于 2015-04-17 00:57:00

sindresorhus/gulp-nunjucks#1中描述的问题相同。

如果你想连接这些文件,你必须设置name选项:

代码语言:javascript
复制
var options = {
  name: function (file) {
    return file.relative;
  }
};
票数 1
EN

Stack Overflow用户

发布于 2015-04-17 01:30:56

我通过执行以下操作解决了这个问题:

  • 使用requireJS

在我的includes

  • setting configure("../html")

  • requiring中正确地重命名*.html

  • removing路径中的模板,如"./subtemplate.txt“
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29676755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档