2013-11-28 06:45:01 +04:00
|
|
|
var templates = {},
|
|
|
|
nodefn = require('when/node/function'),
|
|
|
|
fs = require('fs'),
|
|
|
|
hbs = require('express-hbs'),
|
|
|
|
errors = require('../errorHandling'),
|
|
|
|
path = require('path'),
|
|
|
|
when = require('when'),
|
2013-12-06 12:51:35 +04:00
|
|
|
config = require('../config'),
|
|
|
|
api = require('../api');
|
2013-11-28 06:45:01 +04:00
|
|
|
|
|
|
|
// ## Template utils
|
|
|
|
|
|
|
|
// Compile a template for a handlebars helper
|
|
|
|
templates.compileTemplate = function (templatePath) {
|
|
|
|
return nodefn.call(fs.readFile, templatePath).then(function (templateContents) {
|
|
|
|
return hbs.handlebars.compile(templateContents.toString());
|
|
|
|
}, errors.logAndThrowError);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Load a template for a handlebars helper
|
|
|
|
templates.loadTemplate = function (name) {
|
|
|
|
var templateFileName = name + '.hbs',
|
|
|
|
deferred = when.defer();
|
2013-12-06 12:51:35 +04:00
|
|
|
// Check for theme specific version first
|
|
|
|
return api.settings.read('activeTheme').then(function (activeTheme) {
|
|
|
|
var templatePath = path.join(config.paths().themePath, activeTheme.value, 'partials', templateFileName);
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
// Can't use nodefn here because exists just returns one parameter, true or false
|
|
|
|
fs.exists(templatePath, function (exists) {
|
|
|
|
if (!exists) {
|
|
|
|
// Fall back to helpers templates location
|
|
|
|
templatePath = path.join(config.paths().helperTemplates, templateFileName);
|
|
|
|
}
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
templates.compileTemplate(templatePath).then(deferred.resolve, deferred.reject);
|
|
|
|
});
|
2013-11-28 06:45:01 +04:00
|
|
|
|
2013-12-06 12:51:35 +04:00
|
|
|
return deferred.promise;
|
2013-11-28 06:45:01 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = templates;
|