Get rid of your old-fashioned face furniture and build sleek templates!

What is Shaven?

Shaven is a DOM building utility and template engine based upon JsonML. This simple markup language lets you easily represent XML/HTML with JSON. Templates are therefore simply Javascript arrays.

All code examples with this icon are editable: edit/

compiles to

The first element of the array is the root html element which is going to contain all the child-elements. Those are arrays themselves and are structured as follows:


On the server you can install Shaven with npm:

Then require it in your scripts like this:

In the browser load the browser version of shaven. Get it either from Github or install it with the frontend package manager Bower.

Now you can start using it by passing a JsonML-array to Shaven:

Insert Methods

There are two different methods to insert a DOM-fragment into the DOM.

Firstly you can inject it straight into the DOM:

Secondly you can assign the Shaven-object to a variable and append the fragment later.

To get the HTML-fragment from the Shaven-object reference it with a [0].

Both compiles to:

Syntax Sugar


When you omit the html-tag it uses the default div tag. So div#test ist the same as #test


compiles to


compiles to


When an element with an id is created it automatically gets inserted into the return-object of the shaven function call. Therefore it can get easily referenced later on.

Reference an element without an id by using the $ keyword to assign it a reference-name.


compiles to


HTML-strings are escaped by default.

compiles to

To disable escaping add a & at the end of the element-tag string.

compiles to


Shaven supports different namespaces and makes it therefore possible to create SVGs, MathML and various other XML-based languages.

compiles to

Falsy values

When an element array contains an undefined content-value or no content-value at all, the element will get compiled with an empty body.

When an element contains a null or a false value, the element won't get compiled at all. This is handy for disabling parts of templates or ensuring that no empty container-elements get rendered.


In order to create reusable templates it's best practice to create a function which accepts a data object, inserts the data into the JsonML array and returns it.
Finally iterate over the contacts and use one of the two insertion methods to build the DOM with the contacts.

Callback Functions

You can provide a callback function, which will be called after the element has been created, with the element as the only argument. This functionality is especially suited to add event-listeners.

Fork me on GitHub