plugins can now define a templates directory which gets compiled onload without requiring any hooks

v1.18.x
psychobunny 11 years ago
parent c054ae121b
commit 90a80ad83d

@ -4,6 +4,7 @@ var templates = require('./../../public/src/templates'),
translator = require('./../../public/src/translator'),
utils = require('./../../public/src/utils'),
meta = require('./../meta'),
plugins = require('./../plugins'),
db = require('./../database'),
auth = require('./../routes/authentication'),
async = require('async'),
@ -81,13 +82,14 @@ function routeCurrentTheme(app, themeData) {
}
}
function compileTemplates() {
function compileTemplates(pluginTemplates) {
var mkdirp = require('mkdirp');
winston.info('[themes] Compiling templates');
utils.walk(nconf.get('base_templates_path'), function(err, baseTpls) {
utils.walk(nconf.get('theme_templates_path'), function (err, themeTpls) {
var paths = {};
var paths = pluginTemplates;
baseTpls = baseTpls.map(function(tpl) { return tpl.replace(nconf.get('base_templates_path'), ''); });
themeTpls = themeTpls.map(function(tpl) { return tpl.replace(nconf.get('theme_templates_path'), ''); });
@ -123,7 +125,7 @@ function compileTemplates() {
}
});
});
});
});
}
function handleErrors(err, req, res, next) {
@ -216,7 +218,10 @@ module.exports = function(app, data) {
routeCurrentTheme(app, data.currentThemeData);
routeThemeScreenshots(app, data.themesData);
compileTemplates();
plugins.getTemplates(function(err, pluginTemplates) {
compileTemplates(pluginTemplates);
});
app.use(app.router);

@ -8,6 +8,7 @@ var fs = require('fs'),
db = require('./database'),
meta = require('./meta'),
utils = require('./../public/src/utils'),
pkg = require('../package.json');
(function(Plugins) {
@ -388,7 +389,30 @@ var fs = require('fs'),
});
});
});
}
};
Plugins.getTemplates = function(callback) {
var templates = {};
Plugins.showInstalled(function(err, plugins) {
async.each(plugins, function(plugin, next) {
if (plugin.templates && plugin.id) {
var templatesPath = path.join(__dirname, '../node_modules', plugin.id, plugin.templates);
utils.walk(templatesPath, function(err, pluginTemplates) {
pluginTemplates.forEach(function(pluginTemplate) {
templates[pluginTemplate.replace(templatesPath, '').substring(1)] = pluginTemplate;
});
next(err);
});
} else {
next(false);
}
}, function(err) {
callback(err, templates);
});
});
};
Plugins.showInstalled = function(callback) {
npmPluginPath = path.join(__dirname, '../node_modules');
@ -456,5 +480,5 @@ var fs = require('fs'),
], function(err, plugins) {
callback(err, plugins);
});
}
};
}(exports));

@ -131,7 +131,7 @@ function getModerators(req, res, next) {
function getTemplatesListing(req, res, next) {
utils.walk(nconf.get('views_dir'), function (err, data) {
data = data.concat(require('./plugins').getCustomTemplates())
data = data
.filter(function(value, index, self) {
return self.indexOf(value) === index;
}).map(function(el) {

@ -183,12 +183,4 @@ module.exports = function(app, middleware, controllers) {
setupPluginRoutes(app);
setupPluginAdminRoutes(app);
return {
getCustomTemplates: function() {
pluginRoutes.templates.map(function(tpl) {
return tpl.template;
});
}
};
};
Loading…
Cancel
Save