percento.js
Percento.js
is designed to be Handlebars for JSON.
It is a node module that allows you to template JSON objects with mixins that can be resolved with a context by this package. Mixins are defined using delimiters, the default ones are %...%
.
For example, given the following JSON object (template)
And a context of
singer: "Mraz" actor: "Statham" murderer: "Voorhees"
Percento.js
will produce the following
Installation & Usage
To install this package to your project, run
npm install --save percento
Then require it and use it with
var percento = ; ;
Percento returns the modified string, so you will have to assign it to a variable.
Options
Custom Delimiteres
Percento.js
can be configured with a custom delimiter, or pair of opening and closing delimiters to define your own matchers.
;;
Chaining Resolutions
You can chain resolutions calls together in the following manner
value;
Chaining passes the resolved template through to each subsequent resolve
call. You must call .value()
at the end to return the current resolved template.
Nested Properties
Templates can access nested properties of a context, for example this JSON object
Can access the properties in this context
people: name: "Juliet" surname: "Capulet" name: "Romeo" surname: "Montague"
To pick the values "Juliet"
and "Montague"
.
This uses lodash
's deep get method, so the accessor string in the template should behave the same way.
Self Resolution
Calling percento
with a single argument will try to resolve all mixins using itself as the context, for example
var template = name: "william" surname: "shakespeare" fullname: "%name% %surname%" var result = ; resultfullname; // william shakespeare