Handgrip: Enable generator helpers for Handlebars
Handgrip is a simple wrapper the enables the use of generator inside normal Handelbars helper.
In addition to supporting generator functions, Handgrip keeps the order of executing generator helpers as you write them on template.
Best suited/tested with Koa or co.
Install
$ npm install handgrip
Generator Helpers
This is an example of basic usage of generator helper.
Return a generator function with next
as the single argument, just like Koa’s middleware.
yield next
tells Handgrip to continue parsing the rest of the template.
var hbs = ;var request = ; hbs;
Another interesting example of generator helper is to create composable component with Handgrip/Handlebars helpers. Here, a CSS declaration helper will keep tracks of all component CSS on this page.
{{css "bootstrap"}} ...{{component "list"}}{{component "carousel"}}
var hbs = ; hbs;
Nested generator helper is also supported. Checkout test/
folder to see more example on that.
Normal Helpers
registerGeneratorHelper
does support normal Handlebars helper as well, just like using the plain Handlebars. Here is an example of registered multiple helper
var hbs = ;var request = ; hbs;
Compiling
Compile template string
var handgrip = ; var tpl = handgrip;var body = ;
Handgrip.render
accepts the same parameters and Handlebars.compile
and returns a generator function.
templateFn
to a Handgrip-compatible one
Convert Handblebars compiled var handlebars = ;var handgrip = ; // create a compile templateFn// You could use handgrip.compile(...) as wellvar tpl = handlebars; // convert to to Handgrip-compatiblevar renderer = Handgrip;var body = ;
Handgrip.compile
is the same as Handlebars.compile
.
Implementaion and caveats (Must-see for layout users)
Handgrip keeps an array of generator functions for each render
job.
When Handlebars finishes parsing the template, it replaces those generator helper with placeholder of UUIDs. Then Handgrip begins parsing the generator array and mutate the array as new generator helpers are found, finally Handgrip fills the results back to their corresponding places.
The common way of implementing layout (as found in express-hbs and koa-hbs) is to render the template as string, then create another separate rendering process for layout. Unfornately, this trivial approach is not feasbile with the implementation of Handgrip, since splitting the rendering process means losing track of generator helpers in main template.
However, there is also an easy way of implementing layout with Handgrip.
Layout template:
{{css}}{{js}}...{{__body__}}
Main template:
{{registerCSS "foo"}}{{registerJS "bar"}}{{pageTitle}} {{#layout "layoutName"}} <!-- your content goes here --> ...{{/layout}}
Layout helper (JS):
var hbs = ;hbs; { this__body__ = optionsfn; // read layout file into template string var layoutTpl = ; // this is the same as handlebars.compile(...) return hbs;}
Build
This is normally used on the server side, so there is no minified version (yet).
Tests
Tested on
- Handlebars: 2.0.2, 3.0.3, 4.0.5
- co: 3.0+, 4.0+
- Node: 0.11.14, 4.4.x, 5.x, 6.x
- iojs: 1.0.2 or higher
Run the test
$ npm install$ npm test
License
Copyright (c) 2015 Jingwei "John" Liu
Licensed under the MIT license.