正确的方式来预编译Handlebars模板来运行,就像Jade那样
总之,我需要 – 以编程方式(而不是CLI)把手模板预编译到浏览器端函数,并在浏览器中使用它与handlebars.runtime.js
这样:
html = template_as_fn(data_object);
为什么我需要它 – 我工作在YA ComonJS浏览器打包工具, 成交 ,我也想支持Handlebars。
所以,我寻找正确的方式来预编译Handlebars模板来运行,而不需要eval()
和其他巫术魔法。
就像在这个答案
var data = { name: "Greg" }; var source = "<p>Howdy, {{ name }}</p>"; eval("var templateFunction = " + Handlebars.precompile(source)); var template = Handlebars.template(templateFunction); template(data); => "<p>Howdy, Greg</p>"
让我伤心。
例如,用Jade我可以用这种方式使用文件处理器
'.jade' : (data, filename, cb) -> content = Jade.compile data, _.assign jade_settings, {filename} cb null, "module.exports = #{content}"
有没有可能做一些像手杖一样的玉器?
哦,没关系! 我find了。
Handlebars文件处理器
'.handlebars', (data, filename, cb) -> content = Handlebars.precompile data cb null, "module.exports = #{content}"
并在模块两步骤的解决方法:
renderData : (data) -> template_fn = require './template' # /template.handlebars template = Handlebars.template template_fn res = template data
所以,从现在开始将支持Handlebars 🙂