Skip to content

Comment on Twitter releases new JS template engine: hogan.js

Comments

So let's recap: This is a new Javascript template engine that uses Mustache syntax and allows you to pre-compile template syntax. Sound's good...

But...why didn't they just use Handlebars? Or if you aren't wedded to Mustache syntax, one of the twenty other template engines out there that support pre-compiling?

handlebars is a pretty inelegant lib, a parser generator for a grammar as simple as mustache is just silly, even this current twitter implementation could be significantly smaller. For large deployments like Twitter I imagine these savings are quite important

The Handlebars grammar is more complex than Mustache's grammar. It supports helpers (normal and block), paths, strings, numbers and booleans.

My initial implementation of Handlebars did not include a proper compilation flow, but optimizing its output became very complicated. The new approach (the one used for the past year or so in Handlebars) makes it easier to reason about how the input gets compiled into JavaScript, and each stage in the compilation process provides ways to perform certain kinds of optimizations.

One of the major goals of Handlebars is for users to avoid paying for features they are not using. For instance, the ".." feature, which allows a template to reference a parent context, only impacts the compiled code if it's used.

Finally, for performance-sensitive deployments, Handlebars provides a precompiler that eliminates the need to compile the template on the client. This eliminates the compiler requirement on the client, and only requires a small (1k'ish) library at runtime.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.