template compiling init; changing themes now finally works on this branch

v1.18.x
psychobunny 11 years ago
parent 88e5ddac76
commit 552eb78db8

@ -22,19 +22,19 @@
}; };
templates.get_custom_map = function(tpl) { templates.get_custom_map = function(tpl) {
if (config['custom_mapping'] && tpl) { if (config.custom_mapping && tpl) {
for (var pattern in config['custom_mapping']) { for (var pattern in config.custom_mapping) {
if (tpl.match(pattern)) { if (tpl.match(pattern)) {
return (config['custom_mapping'][pattern]); return (config.custom_mapping[pattern]);
} }
} }
} }
return false; return false;
} };
templates.is_available = function(tpl) { templates.is_available = function(tpl) {
return $.inArray(tpl, available_templates) !== -1; return $.inArray(tpl + '.tpl', available_templates) !== -1;
}; };
templates.prepare = function(raw_tpl) { templates.prepare = function(raw_tpl) {
@ -177,7 +177,7 @@
case 'boolean': case 'boolean':
value = ($(element).val() === 'true' || $(element).val() === '1') ? true : false; value = ($(element).val() === 'true' || $(element).val() === '1') ? true : false;
break; break;
case 'int': // Intentional fall-through case 'int':
case 'integer': case 'integer':
value = parseInt($(element).val()); value = parseInt($(element).val());
break; break;
@ -252,7 +252,6 @@
var template = this.html, var template = this.html,
regex, block; regex, block;
// registering globals
for (var g in templates.globals) { for (var g in templates.globals) {
if (templates.globals.hasOwnProperty(g)) { if (templates.globals.hasOwnProperty(g)) {
data[g] = data[g] || templates.globals[g]; data[g] = data[g] || templates.globals[g];
@ -363,7 +362,14 @@
module.exports.__express = module.exports.render; module.exports.__express = module.exports.render;
if ('undefined' !== typeof window) { if ('undefined' !== typeof window) {
window.templates = module.exports;a window.templates = module.exports;
window.onload = function() {
$.when($.getJSON(RELATIVE_PATH + '/templates/config.json'), $.getJSON(RELATIVE_PATH + '/api/get_templates_listing')).done(function (config_data, templates_data) {
config = config_data[0];
available_templates = templates_data[0];
});
}
} }
})('undefined' === typeof module ? { })('undefined' === typeof module ? {

@ -2,6 +2,7 @@
var templates = require('./../../public/src/templates'), var templates = require('./../../public/src/templates'),
translator = require('./../../public/src/translator'), translator = require('./../../public/src/translator'),
utils = require('./../../public/src/utils'),
meta = require('./../meta'), meta = require('./../meta'),
db = require('./../database'), db = require('./../database'),
auth = require('./../routes/authentication'), auth = require('./../routes/authentication'),
@ -128,6 +129,42 @@ module.exports = function(app, data) {
middleware = require('./middleware')(app); middleware = require('./middleware')(app);
app.configure(function() { app.configure(function() {
utils.walk(nconf.get('base_templates_path'), function(err, baseTpls) {
utils.walk(nconf.get('theme_templates_path'), function (err, themeTpls) {
var tpls = [];
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'), '');
});
baseTpls.forEach(function(el, i) {
var relative_path = (themeTpls.indexOf(el) !== -1 ? themeTpls[themeTpls.indexOf(el)] : baseTpls[i]),
full_path = path.join(themeTpls.indexOf(el) !== -1 ? nconf.get('theme_templates_path') : nconf.get('base_templates_path'), relative_path);
tpls.push({
relative_path: relative_path,
path: full_path
});
});
async.each(tpls, function(tpl, next) {
fs.writeFile(path.join(nconf.get('views_dir'), tpl.relative_path), fs.readFileSync(tpl.path), next);
}, function(err) {
if (err) {
winston.error(err);
} else {
winston.info('Successfully compiled templates.');
}
});
});
});
app.engine('tpl', templates.__express); app.engine('tpl', templates.__express);
app.set('view engine', 'tpl'); app.set('view engine', 'tpl');
app.set('views', nconf.get('views_dir')); app.set('views', nconf.get('views_dir'));

@ -3,6 +3,7 @@
var path = require('path'), var path = require('path'),
async = require('async'), async = require('async'),
fs = require('fs'), fs = require('fs'),
nconf = require('nconf'),
db = require('./../database'), db = require('./../database'),
user = require('./../user'), user = require('./../user'),
@ -22,10 +23,15 @@ module.exports = function(app, middleware, controllers) {
app.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID); app.get('/user/uid/:uid', middleware.checkGlobalPrivacySettings, controllers.accounts.getUserByUID);
app.get('/get_templates_listing', function (req, res) { app.get('/get_templates_listing', function (req, res) {
utils.walk(path.join(__dirname, '../../', 'public/templates'), function (err, data) { utils.walk(nconf.get('views_dir'), function (err, data) {
res.json(data.concat(app.get_custom_templates()).filter(function(value, index, self) { data = data.concat(app.get_custom_templates())
.filter(function(value, index, self) {
return self.indexOf(value) === index; return self.indexOf(value) === index;
})); }).map(function(el) {
return el.replace(nconf.get('views_dir') + '/', '');
});
res.json(data);
}); });
}); });

@ -144,7 +144,7 @@ module.exports = function(app, middleware) {
app.get_custom_templates = function() { app.get_custom_templates = function() {
return custom_routes.templates.map(function(tpl) { return custom_routes.templates.map(function(tpl) {
return tpl.template.split('.tpl')[0]; return tpl.template;
}); });
}; };

Loading…
Cancel
Save