Rendering Trees with Meteor.js + Handlebars

Handlebars is currently the only template engine integrated with Meteor. Notably absent from Handlebar’s feature set is the ability to pass context between blocks. Furthermore, the scope of a parent block is absolutely inaccessible from the child’s context (no super, no this.parent).

One can get around any restriction imposed by Handlebar’s markup by writing a custom block helper. The theory behind block helpers is to keep logic separate from the design markup. Every block in Handlebars (if, each, with, list…) calls a block helper behind the scenes. The block helper is a function which takes the block’s HTML content as an argument, as well as any arguments listed in the block’s declaration, and uses these arguments to return a string which is rendered in place of the block. It’s sort of like a macro.

In the case of a tree, using a block helper is absolutely the way to go.