Jade

Writing HTML can be a chore – making sure elements are closed properly can be annoying without proper IDE support, and CSS markup often requires repeated patterns of nested divs and spans in order to get the layout working properly.

There are various templating languages and engines available for dealing with this problem. Probably my favourite is Jade, due to its minimalist syntax and availability over a bunch of different languages and frameworks. It’s basically like LESS but for HTML. Also, Forbes Lindesay (ex-intern, new developer) is the maintainer!

Jade makes writing HTML a lot more efficient. For example, this:


becomes this:


Most elements are written using just their names, and their contents are indented below them. Elements beginning with a dot or hash are a shorthand for div elements with that class or id respectively.


The other main feature is mixins. These are similar to LESS mixins in that they define pseudo-functions that let you avoid repeated bits of markup. For example,


defines a mixin that could be used like so:


Note the basic control flow available. Jade has similar semantics to javascript – for example, missing parameters are “undefined” and evaluate to false. Jade can also iterate over lists, do switch statements and access properties of objects passed in from the framework hosting it.

Jade also has the ability to define templates by using certain named areas, called blocks. These are used by creating a second file that references the first and defines what content should be in each of the blocks. For example, a layout file might define the general page layout, and put blocks in place of areas such as the navigation and main content. Pages using that layout would then declare that they extend the layout file and just have to define what goes in each block.

Jade can be run locally to generate static html pages, or in a framework such as Flask or Django (for Python) or Express (for Node.js). There is even an (apparently limited) implementation for ASP.NET MVC in the Spark view engine.

Useful links
Jade language reference (with online demo): jade-lang.com/
Spark view engine: nuget.org/packages/Spark/
with some examples of using Jade vs. Razor in Spark: gist.github.com/loudej/2371061
and the SQL Automation Pack Intro app, which we wrote using Jade: tinyurl.com/sqlautomationpack