Oct 26, 2018

JavaScript templating solutions

There are many open source templating solutions to choose from including:

- Mustache available in Ruby, JavaScript, Python, Erlang, node.js, PHP, Perl, Perl6, Objective-C, Java, .NET, Android, C++, Go, Lua, ooc, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, Haskell, XQuery, ASP, Io, Dart, Haxe, Delphi, Racket, Rust, and C#. I like the logic-less approach of a mustache template as it encourages a separation of concerns. Also, having the template language in both client-side and the server-side language of your choice is a huge win for template reuse, consistency, and maintainability.

- Handlebars is basically a superset of Mustache (above) with the goal of putting a small amount of logic back into the logic-less templates. For example, you can register custom methods to help process the JSON data. As well as the ability to change the current context by using paths such as ../ to change to the parent context. As for using it server-side, you are restricted to Node.js, but it is possible - by pre-compiling the templates - to use the same templating language on both the client and server.

- Dust.js is currently my favorite client-side templating language. Dust takes the best of Mustache and Handlebars. Like Handlebars, it extends the Mustache syntax with path support and helper functions, while adding in a few features of its own, such as asynchronous rendering, built in filters, support for streaming, and inline parameters while still remaining highly performant.