【问题标题】:Handlebars compiles an empty template without data running inside Gulp taskHandlebars 编译一个没有数据在 Gulp 任务中运行的空模板
【发布时间】:2018-07-13 22:38:15
【问题描述】:

我正在运行一个简单的 gulp 任务,该任务应该以特定语言呈现我的车把模板版本。语言数据存储在 JSON 文件中,我需要在 gulp 管道任务中动态地将其存储在变量中,然后执行把手任务。

gulp.task('compilen', () => {
var strings;


return gulp.src('frontend/templates/*/*.hbs')
.pipe(through.obj(function(file,enc,cb){
    var p = parsePath(file.relative);

    strings = require('./frontend/templates/' + p.dirname+ '/locales/en.json');

    console.log(strings);

     cb(null,file);
}))
.pipe(handlebars(strings, options))
.pipe(rename(function(path){


    path.basename += "-en";
    path.extname = ".html";

}))
.pipe(gulp.dest('build'));

});

每当我运行gulp 时,一切都会运行,它会输出一个没有strings 数据的文件。 {{ }} 已删除,但没有实际数据,就好像字符串对象为空一样,但情况并非如此,因为每当我用 tap 和 console.log 包装 handlebars 函数时,它们都在那里.. 什么是更奇怪的是,如果我要传递一个对象字面量,而不是 strings 变量,一切都会正确呈现。

这是一个示例 hbs 模板和我的 json 文件

main.hbs

{{> header}}

<div>
  {{home.title}}
</div>

en.json

{
    "home" : {
    "title" : "hello world",
    "heading" : "1245"
    }
 }

main.html 输出

<div>

</div>
<div>

</div>

【问题讨论】:

    标签: gulp handlebars.js gulp-compile-handlebars


    【解决方案1】:

    我通过使用文档底部的另一个插件 gulp-data 参考解决了这个问题。我用gulp-data 对象替换了through.obj,它现在可以工作了。我一直在努力尝试用 gulp 做一些简单的事情,所以我的建议是远离。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-02
      • 2017-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多