Handlebars precompiler plugin for Browserify without magic.
Compiles Handlebars templates to plain Javascript. The compiled templates only have one copy of the Handlebars runtime so they are lightweight and fast!
Install hbsfy locally to your project:
npm install hbsfy
Handlebars will be automatically installed as peer dependency.
Then use it as Browserify transform module with -t
:
browserify -t hbsfy main.js > bundle.js
where main.js can be like:
var template = require("./template.hbs");
document.body.innerHTML = template({ name: "Epeli" });
and template.hbs:
<h1>Hello {{name}}!</h1>
When compiling using Javascript code custom extensions can be set:
var hbsfy = require("hbsfy").configure({
extensions: ["html"]
});
var browserify = require("browserify");
var b = browserify("./index.js");
b.transform(hbsfy);
b.bundle().pipe(fs.createWriteStream("./bundle.js"));
To register custom helpers just require the runtime use and registerHelper
to
create helper:
var Handlebars = require("hbsfy/runtime");
Handlebars.registerHelper("upcase", function(s) {
return s.toUpperCase();
});
Partials can be created by giving precompiled template to the registerPartial
function.
Handlebars.registerPartial('link', require("./partial.hbs"));
Checkout the example folder for details.
- Support Handlebars 1.3
- Now uses the official runtime api
- Remove
handlebars-runtime
dependency and depend directly on thehandlebars
module as a peer dependency.- Runtime must be now required with
require("hbsfy/runtime")
instead ofrequire("handlebars-runtime")
. - Thanks to @kamicane for teaching me how to do this.
- Runtime must be now required with
- Option to configure template extensions