2013-11-27 21:45:01 -05:00
|
|
|
var templates = {},
|
|
|
|
hbs = require('express-hbs'),
|
2014-05-09 05:11:29 -05:00
|
|
|
errors = require('../errors');
|
2013-11-27 21:45:01 -05:00
|
|
|
|
|
|
|
// ## Template utils
|
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
// Execute a template helper
|
|
|
|
// All template helpers are register as partial view.
|
2015-03-28 11:00:57 -05:00
|
|
|
templates.execute = function (name, context, options) {
|
2013-12-01 18:31:55 -05:00
|
|
|
var partial = hbs.handlebars.partials[name];
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
if (partial === undefined) {
|
|
|
|
errors.logAndThrowError('Template ' + name + ' not found.');
|
|
|
|
return;
|
|
|
|
}
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2013-12-01 18:31:55 -05:00
|
|
|
// If the partial view is not compiled, it compiles and saves in handlebars
|
|
|
|
if (typeof partial === 'string') {
|
2014-01-23 11:22:25 -05:00
|
|
|
hbs.registerPartial(partial);
|
2013-12-01 18:31:55 -05:00
|
|
|
}
|
2013-11-27 21:45:01 -05:00
|
|
|
|
2015-03-28 11:00:57 -05:00
|
|
|
return new hbs.handlebars.SafeString(partial(context, options));
|
2013-11-27 21:45:01 -05:00
|
|
|
};
|
|
|
|
|
2014-09-09 23:06:24 -05:00
|
|
|
module.exports = templates;
|