-
Notifications
You must be signed in to change notification settings - Fork 45
Templating
kkaefer edited this page May 16, 2011
·
1 revision
Bones uses Underscore's _.template()
function as a default template engine. To invoke partials, use this['PartialName'](obj)
.
To add another template engine, add this code to your index.js
or any other file that is loaded before the call to require('bones').load(__dirname)
:
require.extensions['.mytemplate'] = function(module, filename) {
var content = fs.readFileSync(filename, 'utf8');
var name = path.basename(filename).replace(/\..+$/, '');
try {
// Replace this with whatever your template engine requires to compile
// templates. Be sure to assign the function to module.exports
module.exports = mytemplate.compile(content);
Bones.plugin.add(module.exports, filename);
} catch (err) {
// Show error message when template compilation failed.
var lines = err.message.split('\n');
lines.splice(1, 0, ' in template ' + filename);
err.message = lines.join('\n');
throw err;
}
// The .register() method is called when the template is added to a server
module.exports.register = function(app) {
if (app.assets) {
app.assets.templates.push({
filename: filename,
// Replace this with whatever you want to send to the client.
content: 'template = ' + module.exports + ';'
});
}
};
};
Bones supports all template engines that can be compiled into a function. The resulting function accepts a hash containing parameters for that template as the first parameter.