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 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:
There are three different methods to add a DOM-fragment to a document.
To get the HTML-fragment from the Shaven-object reference it with
Both compiles to:
innerHTMLof an element.
To get the HTML string of a Shaven-object simply call the
toString() method, convert the object to a String
String(shavenObject) or the easiest way -
add it to another string.
When you omit the html-tag it uses the default
div#container ist the same as
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
to assign it a reference-name.
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
false the attribute
is not created.
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
undefined are deleted from the final style-string.
If a transform attribute is an an array of objects it will get compiled to an SVG transform string.
HTML-strings are escaped by default.
To disable escaping add a
! at the end of the
When an element array contains an
or no content-value at all, the element will get compiled with an
When an element contains a
null or a
the element won't get compiled at all. This is handy for disabling
of templates or ensuring that no empty container-elements get
true value does therefore not change the
An array which consists exclusively of subarrays will get merged into the current array
In order to create reusable templates it's best practice to create a
which accepts a data object, inserts the data into the JsonML array
Finally iterate over the contacts and use one of the two insertion methods to build the DOM with the contacts.
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.
Shaven can be configured at module scope and settings can be overwritten at each invocation. Available settings are:
To overwrite the default settings use
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:
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.