Dynamic Inset Renderer for Node.js
Server-side inset.json rendering
Based on the dynamic inset renderer, this library implements the most basic aspects of the inset.json spec.
Installation
npm install dynamic-inset
Requirements
Dynamic insets require a discoverable inset.json file (that conforms to the inset.json spec). The inset should have either additional discoverable assets, or in-line data and/or templates to use to create the rendered inset.
The default renderer for insets is hogan, but the advanced example in this documentation shows how the rendering engine can be swapped out for another mustache renderer, or a different templating syntax entirely.
Usage
//include the modulevar inset = ; //request the insetinset;
The result of results.compiled_inset
is a fully-rendered string of html code, ready to be inserted into a document.
The results
object is a collection of the responses to each step of the async processing on the various parts of
the inset processing steps:
results.compiled_inset
First InsetThe content of this inset is from a json file. and the template is from a mustache file. dog cat goat
The complete results object
inset_file: status: 'OK' type: 'InsetDynamic' platforms: 'desktop' serverside: data: Object template: Object sharing: false inset_template: '<h1>{{title}}</h1>\n<p>{{importantstuff}}</p>\n{{#list.length}}\n<ul>\n{{#list}}<li>{{.}}</li>{{/list}}\n</ul>{{/list.length}}\n' inset_data: title: 'First Inset' list: 'dog' 'cat' 'goat' importantstuff: 'The content of this inset is from a json file. and the template is from a mustache file.' compiled_inset: '<h1>First Inset</h1>\n<p>The content of this inset is from a json file. and the template is from a mustache file.</p>\n<ul>\n<li>dog</li><li>cat</li><li>goat</li>\n</ul>\n' status:'OK'}
Alternate usage
The inset object's url property can be set manually before execution, as a convenience.
//include the modulevar inset = ; //set the urlinseturl = 'http://dynamic-inset-assets.herokuapp.com/inset/inset.json'; //render, using only a function as a paraminset;
Inline inset usage
A string of an inset can be passed to the renderer to render directly.
var insetString = JSON; inset;
Status
Insets can be remotely disabled via the status property in the specification. The inset.json file's status is bubbled up to the top-level of the object for easy discovery and testing.
//set the url
inset.url = 'http://dynamic-inset-assets.herokuapp.com/inset/inset.json';
inset.render(function(err, results){
if(results.status == 'OK') {
//process the inset because it is ok
} else {
//ignore the inset, as it has been disabled remotely.
}
});
Advanced use-cases
Individual functions can be overloaded with alternatives. In this example, hogan is swapped out for the standard mustache renderer.
var inset = mustache = ; //overload the compile function with an alternate rendererinset {