2013-03-17 50 views
2

总之,我需要什么 - 以编程方式预编译(不CLI)车把模板浏览器端的功能,并在浏览器以这种方式与handlebars.runtime.js使用它:预编译的手把模板正确的方式运作,因为玉做

html = template_as_fn(data_object); 

为什么我需要它 - 我使用YA ComonJS浏览器打包工具,clinch,我也想支持Handlebars。

所以,我正在寻找正确的方式来预编译Handlebars模板来运行,没有eval()和其他巫术魔法。

事情就像这个answer

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' : (data, filename, cb) -> 
    content = Jade.compile data, _.assign jade_settings, {filename} 
    cb null, "module.exports = #{content}" 

使用file processor是否有可能像做翡翠手把用?

回答

1

哦,没关系!我找到了。

的把手

'.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 

所以,从现在clinch将支持把手:)