$ brew install node
$ npm install handlebars -g
-g
flag installs the package globally, so it can be used in any project. $ handlebars <input> -f <output>
Handlebars.templates
. If your input file is person.handlebars
, the compiler will insert it at Handlebars.templates.person
. This template will be a function that may be directly executed in the same manner as templates compiled locally. I.e. Handlebars.templates.person(context, options);
<script src="/libs/handlebars.runtime.js"></script>
handlebars <input> -f <output> -k each -k if -k unless
--knownOnly
option provides the smallest generated code that also provides the fastest execution. Usage: node ./bin/handlebars [template|directory]... Options: -f, --output Output File --map Source Map File [string] [default: undefined] -a, --amd Exports amd style (require.js) -c, --commonjs Exports CommonJS style, path to Handlebars module [default: null] -h, --handlebarPath Path to handlebar.js (only valid for amd-style) [default: ""] -k, --known Known helpers -o, --knownOnly Known helpers only -m, --min Minimize output -n, --namespace Template namespace [default: "Handlebars.templates"] -s, --simple Output template function only. -N, --name Name of passed string templates. Optional if running in a simple mode. Required when operating on multiple templates. -i, --string Generates a template from the passed CLI argument. "-" is treated as a special value and causes stdin to be read for the template value. -r, --root Template root. Base value that will be stripped from template names. -p, --partial Compiling a partial template -d, --data Include data when compiling -e, --extension Template extension. [default: "handlebars"] -b, --bom Removes the BOM (Byte Order Mark) from the beginning of the templates. -v, --version Prints the current compiler version --help Outputs this message
Handlebars.templates
object using the relative template name sans the extension. These templates may be executed in the same manner as templates. Handlebars.template
method and the resulting object may be used as normal. var template = "<div>whatever {{ name }}</div>"; var Handlebars = require("handlebars"); var compiled = Handlebars.precompile(template);
var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {}; Handlebars.partials["test1"] = template({"compiler":[7,">= 4.0.0"],"main":function(container,depth0,helpers,partials,data) { var helper; return "<div>whatever " + container.escapeExpression(((helper = (helper = helpers.name || (depth0 != null ? depth0.name : depth0)) != null ? helper : helpers.helperMissing),(typeof helper === "function" ? helper.call(depth0 != null ? depth0 : (container.nullContext || {}),{"name":"name","hash":{},"data":data}) : helper))) + "</div>"; },"useData":true});
var result = Handlebars.partials["test1"]({name : "yourname"}); //do whatever you want with the result
© 2011–2017 by Yehuda Katz
Licensed under the MIT License.
https://handlebarsjs.com/precompilation.html