【问题标题】:using express-hbs to precompile static pages使用 express-hbs 预编译静态页面
【发布时间】:2015-09-28 16:53:46
【问题描述】:

我正在尝试使用 express-hbs 在 gulp 任务中预编译一些静态 HTML(带有布局和部分),作为“发布此库”任务的一部分。我有一些看起来像

gulp.task 'compile', ->
  gulp.src './www/views/*.html'
    .pipe through.obj (file, enc, cb) ->
      //the missing part
    .pipe gulp.dest './temp'

我知道我可以调用 hbs compile 来编译单个文件,但这不包括任何部分或布局。

template = hbs.compile(file.contents.toString());
file.contents = new Buffer(template(data));
this.push(file);
cb();

我知道我只是在这里遗漏了一些简单的东西,查看 express-hbs 的来源我不确定我需要传递给该渲染函数以使其做正确的事情。

我想使用 express.hbs 插件,因为我们正在使用它添加到车把的一些帮助程序,例如 contentFor 等。

更新,解决方案:

gulp.task 'compile', ->
  gulp.src './www/views/*.html'
    .pipe through.obj (file, enc, cb) ->
      render = hbs.create().express3
        viewsDir: __base + 'views'
        partialsDir: __base + 'views/partials'
        layoutDir: __base + 'views/layouts'
        defaultLayout: __base + 'views/layouts/layout.html'
        extName: 'html'

      locals = {
        settings: {
          views: __base + 'views'
        }
      }

      self = this;
      render file.path, locals, (err, html) ->
        if(!err)
          file.contents = new Buffer(html);
          self.push(file);
          cb();
        else
          console.log "failed to render #{file.path}"
    .pipe gulp.dest './temp'

【问题讨论】:

    标签: node.js express gulp handlebars.js


    【解决方案1】:

    这里有一个名为 nonExpress 的 express-hbs 存储库中的测试,它看起来与您想要的类似。

    https://github.com/barc/express-hbs/blob/master/test/nonExpress.js

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2018-11-08
      • 2018-01-20
      • 1970-01-01
      • 2018-05-26
      相关资源
      最近更新 更多