0.0.7 • Published 10 years ago
sidebars v0.0.7
Asynchronous sidebar / widget execution library
Install
npm install sidebars
How To Use
Define a template, set of sidebars and widgets.
In this example we defined two sidebars (header and footer), and we resued the same widget "mywidget" three times with different values.
var sidebars = require('sidebars');
sidebars
.render(req, res, next, {
template: 'hello.br',
sidebars: {
header: [
{
package : 'mywidget', // same as require('mywidget')
params : { title: "Header Goes Here" } // key value pairs to send to this specific widget
}
],
footer: [
{
package : 'mywidget',
params : { title: "Footer Goes Here" }
},
{
package : 'mywidget',
params : { title: "Sub Footer Goes Here" }
}
]
}
});
Write a sample widget that returns template and data to render the widget.
var mywidget = module.exports = {
execute: function (params, req, cb) {
// Do some logic here
cb(null, {
template : 'widget.br',
data : params
});
}
}
Write a template layout that defines the placement of the sidebars (hello.br)
<html>
<!-- define all sidebars with tripple braces -->
<body>
<header>
{{{header}}}
</header>
<footer>
{{{footer}}}
</footer>
</body>
</html>
Write a template for the widget (widget.br)
<h1>{{title}}</h1>
The final result will be
<html>
<body>
<header>
<h1>Header Goes Here</h1>
</header>
<footer>
<h1>Footer Goes Here</h1>
<h1>Sub Footer Goes Here</h1>
</footer>
</body>
</html>
Extend
Use handlebar template engine.
var sidebars = require('sidebars');
sidebars
.engine( sidebars.handlebars )
.render(req, res, next, {...});
Add a plugin debug.js
var sidebars = require('sidebars');
sidebars
.use( sidebars.debug )
.render(req, res, next, {...});