kobayashi
v1.0.1
Published
Asynchronous wrapper for Mustache.js
Maintainers
Readme
Kobayashi
Kobayashi is an asynchronous wrapper for Mustache.js. Adding support for loading files and using layouts.
Kobayashi requires a nodejs version that supports Promise & async/await).
npm install kobayashiQuick start
const Kobayashi = require('kobayashi');
let file = Kobayashi.load('./index.html'); // returns a promise, which is later resolved in the render function.
let partial1 = Kobayashi.load('./partial1.html');
let layout = Kobayashi.load('./layout.html');
Kobayashi.render(file, { hello: 'world'}, { partial1, partial2: 'Hello.' }, layout).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});Documentation
async render(template, view = {}, partials = {}, layout)
Renders the template with the view. You can also supply partials and layout. When rendering with a layout the {{{body}}}-tag is the insertion point, and thus a reserved tag.
Returns a Promise which resolves with the resulting rendering.
Parameters:
template: String or Promiseview: Objectpartials: Object with Strings or Promiseslayout: String or Promise
Example:
let file = Kobayashi.load('./index.html'); // returns a promise, which is later resolved in the render function.
let partial1 = Kobayashi.load('./partial1.html');
let layout = Kobayashi.load('./layout.html');
Kobayashi.render(file, { hello: 'world'}, { partial1, partial2: 'Hello.' }, layout).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});load(path)
Loads file from specified path.
Returns a Promise which resolves with the content of the file.
Example:
let file = Kobayashi.load('./index.html'); // returns a promise, which is later resolved in the render function.
Kobayashi.render(file, { hello: 'world'}).then(result => {
console.log(result);
}).catch(error => {
console.log(error);
});async inject(source, destination, tag)
Injects the source template into a destination template at the specified tag.
Returns a Promise which resolves with the resulting template.
Parameters:
source: String or Promisedestination: String or Promisetag: String
Example:
let source = '<h1>Hello world!</h1>';
let destination = '<body>{{test}}<div>{{wow}}</div></body>';
Kobayashi.inject(source, destination, 'test').then(result => {
console.log(result);
// logs: <body><h1>Hello world!</h1><div>{{wow}}</div></body>.
}).catch(error => {
console.log(error);
});
