2013-11-28 06:45:01 +04:00
|
|
|
var templates = {},
|
|
|
|
hbs = require('express-hbs'),
|
2013-12-02 03:31:55 +04:00
|
|
|
errors = require('../errorHandling');
|
2013-11-28 06:45:01 +04:00
|
|
|
|
|
|
|
// ## Template utils
|
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// Execute a template helper
|
|
|
|
// All template helpers are register as partial view.
|
|
|
|
templates.execute = function (name, context) {
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
var partial = hbs.handlebars.partials[name];
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
if (partial === undefined) {
|
|
|
|
errors.logAndThrowError('Template ' + name + ' not found.');
|
|
|
|
return;
|
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
// If the partial view is not compiled, it compiles and saves in handlebars
|
|
|
|
if (typeof partial === 'string') {
|
|
|
|
partial = hbs.handlebars.compile(partial);
|
|
|
|
hbs.handlebars.partials[name] = partial;
|
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-02 03:31:55 +04:00
|
|
|
return new hbs.handlebars.SafeString(partial(context));
|
2013-11-28 06:45:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = templates;
|