使用Brunch.io编译handlebars模板

compile handlebars template with Brunch.io

本文关键字:handlebars 模板 编译 io Brunch 使用      更新时间:2023-09-26

我用的是早午餐。io和Handlebars插件。为了编译我的模板,我需要得到模板和我所理解的,我应该能够做到这一点与require()

这样就可以了:

var tmp = require('./templates/projects');
var template = Handlebars.compile(tmp);
var compiledHtml = template(data); //data is a var with data for the template inside
viewEl.innerHTML = template; //viewEl is id of div where i want to show the tmpl

但是如果我这样做,我在构建时不会得到错误,而是在浏览器检查时得到它:

initialize.js:52 Uncaught Error: Cannot find module 'templates/projects' from 'initialize.js'

其中initialize.js是我的主源js文件。

根据我的理解,它不起作用,因为我的模板没有按照我的配置文件在public/javascript/app.js下的公共文件夹中编译:

module.exports = {
  // See http://brunch.io for documentation.
  files: {
    javascripts: {
        joinTo: {
            'js/app.js': /^app/
        }
    },
    stylesheets: {joinTo: 'css/main.css'},
    templates: { joinTo: 'js/app.js'}
  }
}

还是我真的错过了什么?

找到问题。

看起来handlebars-brunch插件和static-handlebars-brunch插件不能一起工作。

一旦我卸载static-handlebars-brunch,我可以看到我的handlebars模板编译到我的app.js