dynamic-inset

0.2.2 • Public • Published

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 module
var inset = require('dynamic-inset');
 
//request the inset
inset.render("http://dynamic-inset-assets.herokuapp.com/inset/inset.json", function(err, results){
  //do something with the inset
  console.log(results.compiled_inset);
});

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

<h1>First Inset</h1>
<p>The content of this inset is from a json file. and the template is from a mustache file.</p>
<ul>
  <li>dog</li>
  <li>cat</li>
  <li>goat</li>
</ul>

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 module
var inset = require('dynamic-inset');
 
//set the url
inset.url = 'http://dynamic-inset-assets.herokuapp.com/inset/inset.json';
 
//render, using only a function as a param
inset.render(function(err, results){
  console.log(results.compiled_inset);
});

Inline inset usage

A string of an inset can be passed to the renderer to render directly.

var insetString = JSON.stringify({
  "status": "OK",
  "type": "InsetDynamic",
  "platforms": ["desktop"],
  "serverside": {
    "data": {
      "data":{"title":"hello"}
    },
    "template": {
      "template": "<h1>{{title}}</h1>"
    }
  }
});
 
inset.render(insetString, function(err, results){
  console.log(results.compiled_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 = require('dynamic-inset')
  , mustache = require('mustache');
 
//overload the compile function with an alternate renderer
inset.compile = function(callback, results) {
  callback(null, mustache.render(results.inset_template, results.inset_data);
}
 
//request the inset
inset.render("http://dynamic-inset-assets.herokuapp.com/inset/inset.json", function(err, results){
  //output will be rendered with mustache, instead of hogan
  console.log(results.compiled_inset);
});

Readme

Keywords

Package Sidebar

Install

npm i dynamic-inset

Weekly Downloads

1

Version

0.2.2

License

MIT

Last publish

Collaborators

  • everyplace