Reputation: 345
In short, what I need - to precompile programmatically (not CLI) handlebars template to browser-side function and at browser use it with handlebars.runtime.js
in this way:
html = template_as_fn(data_object);
Why I need it - I work on YA ComonJS to browser packer tool, clinch and I want to support Handlebars too.
So, I looking for correct way to precompile Handlebars template to function, without eval()
and other voodoo magic.
Things like in this 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>"
make me sad.
For example, with Jade I can use file processor in this way
'.jade' : (data, filename, cb) ->
content = Jade.compile data, _.assign jade_settings, {filename}
cb null, "module.exports = #{content}"
Is it possible to do something like Jade with Handlebars?
Upvotes: 2
Views: 406
Reputation: 345
Oh, nevermind! I find it.
file processor for Handlebars
'.handlebars', (data, filename, cb) ->
content = Handlebars.precompile data
cb null, "module.exports = #{content}"
And in module two-step workaround:
renderData : (data) ->
template_fn = require './template' # /template.handlebars
template = Handlebars.template template_fn
res = template data
So, from now clinch will support Handlebars :)
Upvotes: 1