
Node.js Self Templated project
Self Templated brings the power of templating to your literal object with a graph resolution of the references. It traverses an object recursively and use the same self-referenced object as a context. It is entirely agnostic of the templating engine being used and default to Handlebars.
If this is not clear, imagine a templating engine rendering all the string of an object and inject that same object as a context.
If this is still not clear, imagine a configuration where each value can referenced other values from that same configuration, see the example below.
If you have understood but a more sarrow introduction would have helped, please share your suggestions.
This package features:
- Agnostic/multi template engines
- Circular refenrences detection
- Simple and consise API
- Full test coverage
Installation
This is OSS and licensed under the new BSD license. The project
homepage is located on GitHub and the package is distributed by NPM:
npm install templated-object
Usage
templated(object[,options])
Quite simple, the exported module is a function taking the object
to render as
first argument and optionally an options
object as second argument.
Options includes:
array
(boolean)
Proxify array as well, default is false
compile
(boolean)
Resolve all the template on creation instead of on-demand, default is false
.handlebars
(object)
Options passed to HandleBars.partial
(object)
Filtering the templating to a restricuted list of properties. It is an object
where keys can be properties or indexes and values activate or desactivate
the template rendering.render
(function)
A user defined function responsible to render a template. Argments are the template and the context, expect to returned the rendered result. Default implementation is using HandleBars.
Examples
templated = require('templated-object');
config = templated({
"webapp": {
"app_name": 'cool_app',
"db_url": 'mysql://{{db.host}}:{{db.port}}/{{webapp.db_name}}',
"db_name": 'db_{{webapp.app_name}}'
},
"db": {
"host": 'localhost',
"port": '3306'
}
});
console.log(config.webapp.db_url == "mysql://localhost:3306/db_cool_app");
Development
Tests are executed with mocha. To install it, simple run npm install
, it will install
mocha and its dependencies in your project "node_modules" directory.
To run the tests:
npm test
The tests run against the CoffeeScript source files.
The test suite is run online with Travis.
Contributors