precompile handlebar templates

2019-03-21 02:24发布

问题:

I'm wondering what's the best way to save precompiled handlebar templates and include them in an HTML file.

The site: http://handlebarsjs.com/precompilation.html simply advises to call

handlebars infile -f outfile

I put a template in a file and called the command. I got a JS file containing a function program1 How would I now compile a second template? The sample at http://jsperf.com/jquery-template-vs-handlebars contains multiple functions named program[1-9](...) I assume they somehow must be generated. Is it good advice to store precompiled templates at all? I saw samples embedding script or link tags in an HTML file and traversing these and via AJAX loading the templates and compiling them dynamically.

Best, Bernhard

回答1:

If you want multiple files, list them in the command like so

handlebars file1 file2 file3 -f outfile

You DO want to store precompiled files for your production site as it drastically cuts down on load time.



回答2:

You should use

handlebars /path/to/templates/*.handlebars -f /path/to/output/templates.js


回答3:

Easiest way, provided that you want to keep your templates with html extension is this: handlebars html

that is: handlebars ./ -e html -f precompiled_templates.js