allow recursive base templates

ex. custom theme based off lavender which is based off vanilla
v1.18.x
psychobunny 9 years ago
parent 759b183967
commit da5494fdf1

@ -12,7 +12,8 @@ var mkdirp = require('mkdirp'),
plugins = require('../plugins'), plugins = require('../plugins'),
utils = require('../../public/src/utils'), utils = require('../../public/src/utils'),
Templates = {}; Templates = {},
searchIndex = {};
Templates.compile = function(callback) { Templates.compile = function(callback) {
callback = callback || function() {}; callback = callback || function() {};
@ -27,21 +28,32 @@ Templates.compile = function(callback) {
return callback(); return callback();
} }
var coreTemplatesPath = nconf.get('core_templates_path'), compile(callback);
baseTemplatesPath = nconf.get('base_templates_path'), };
viewsPath = nconf.get('views_dir'),
themeTemplatesPath = nconf.get('theme_templates_path'),
themeConfig = require(nconf.get('theme_config'));
if (themeConfig.baseTheme) {
var pathToBaseTheme = path.join(nconf.get('themes_path'), themeConfig.baseTheme);
baseTemplatesPath = require(path.join(pathToBaseTheme, 'theme.json')).templates;
if (!baseTemplatesPath){ function getBaseTemplates(theme) {
baseTemplatesPath = path.join(pathToBaseTheme, 'templates'); var baseTemplatesPaths = [],
} baseThemePath, baseThemeConfig;
while (theme) {
baseThemePath = path.join(nconf.get('themes_path'), theme);
baseThemeConfig = require(path.join(baseThemePath, 'theme.json'));
baseTemplatesPaths.push(path.join(baseThemePath, baseThemeConfig.templates || 'templates'));
theme = baseThemeConfig.baseTheme;
} }
return baseTemplatesPaths.reverse();
}
function compile(callback) {
var coreTemplatesPath = nconf.get('core_templates_path'),
themeConfig = require(nconf.get('theme_config')),
baseTemplatesPaths = themeConfig.baseTheme ? getBaseTemplates(themeConfig.baseTheme) : [nconf.get('base_templates_path')],
viewsPath = nconf.get('views_dir');
plugins.getTemplates(function(err, pluginTemplates) { plugins.getTemplates(function(err, pluginTemplates) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -54,27 +66,33 @@ Templates.compile = function(callback) {
coreTpls: function(next) { coreTpls: function(next) {
utils.walk(coreTemplatesPath, next); utils.walk(coreTemplatesPath, next);
}, },
baseTpls: function(next) { baseThemes: function(next) {
utils.walk(baseTemplatesPath, next); async.map(baseTemplatesPaths, function(baseTemplatePath, next) {
utils.walk(baseTemplatePath, function(err, paths) {
paths = paths.map(function(tpl) {
return {
base: baseTemplatePath,
path: tpl.replace(baseTemplatePath, '')
};
});
next(err, paths);
});
}, next);
} }
}, function(err, data) { }, function(err, data) {
var coreTpls = data.coreTpls, var baseThemes = data.baseThemes,
baseTpls = data.baseTpls, coreTpls = data.coreTpls,
paths = {}; paths = {};
if (!baseTpls) {
winston.warn('[meta/templates] Could not find base template files at: ' + baseTemplatesPath);
}
coreTpls = !coreTpls ? [] : coreTpls.map(function(tpl) { return tpl.replace(coreTemplatesPath, ''); });
baseTpls = !baseTpls ? [] : baseTpls.map(function(tpl) { return tpl.replace(baseTemplatesPath, ''); });
coreTpls.forEach(function(el, i) { coreTpls.forEach(function(el, i) {
paths[coreTpls[i]] = path.join(coreTemplatesPath, coreTpls[i]); paths[coreTpls[i].replace(coreTemplatesPath, '')] = path.join(coreTemplatesPath, coreTpls[i]);
}); });
baseTpls.forEach(function(el, i) { baseThemes.forEach(function(baseTpls) {
paths[baseTpls[i]] = path.join(baseTemplatesPath, baseTpls[i]); baseTpls.forEach(function(el, i) {
paths[baseTpls[i].path] = path.join(baseTpls[i].base, baseTpls[i].path);
});
}); });
for (var tpl in pluginTemplates) { for (var tpl in pluginTemplates) {
@ -125,9 +143,8 @@ Templates.compile = function(callback) {
}); });
}); });
}); });
}; }
var searchIndex = {};
function addIndex(path, file) { function addIndex(path, file) {
searchIndex[path] = file; searchIndex[path] = file;

Loading…
Cancel
Save