Shaven

2.1.0

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 on JsonML. This simple markup language lets you easily represent XML/HTML with JSON and plain JavaScript objects. Templates are therefore simply a tree of nested arrays.

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:

Installation

On the server you can install Shaven with npm:

Then require it in your scripts like this:

In the browser use a bundler or load the pre-compiled version of Shaven from npmjs.com/package/shaven

Now you can start using it by passing an array to Shaven:

Insert Methods

There are three different methods to add a DOM-fragment to a document.

1. Inject it straight into the DOM:

2. Assign the Shaven-object to a variable and append the fragment later.

To get the HTML-fragment from the Shaven-object reference it with .rootElement.

Both compiles to:

3. Set the innerHTML of an element.

To get the HTML string of a Shaven-object simply call the toString() method, convert the object to a String with String(shavenObject) or the easiest way - add it to another string.

Syntax Sugar

Tag

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

compiles to

Id

compiles to

Class

compiles to

References

When an element with an id is created it automatically gets inserted into the corresponding property in 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.

Properties

All entries of an object become the attributes of the element. If an object entry is undefined the attribute is set to an empty string. If an entry is null or false the attribute is not created.

Style Attribute

If the style entry is an object it will automatically get compiled to a string. This is especially useful for working with svgs, where inline styles are still frequently used. Values which are false, null or undefined are deleted from the final style-string.

compiles to

Transform Attribute

If a transform attribute is an an array of objects it will get compiled to an SVG transform string.

compiles to

Escaping

HTML-strings are escaped by default.

compiles to


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

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. (A true value does therefore not change the compilation.)

Subarrays

An array which consists exclusively of subarrays will get merged into the current array

compiles to

Templates

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.

Configuration

Shaven can be configured at module scope and settings can be overwritten at each invocation. Available settings are:

To overwrite the default settings use setDefaults(). This will be set at the module scope for every invocation of shaven.

To overwrite the settings for only one element use the object invocation mode of shaven:

compiles to

Namespaces

Shaven automatically uses the correct namespaces to create HTML, SVG, and MathML. It is, however, also possible to use a custom namespace to create documents in various other XML-based languages or to overwrite the defaults.

compiles to

Fork me on GitHub