mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-31 19:15:58 +01:00
template compiling init; changing themes now finally works on this branch
This commit is contained in:
@@ -17,27 +17,27 @@
|
|||||||
path = require('path');
|
path = require('path');
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
templates.force_refresh = function (tpl) {
|
templates.force_refresh = function(tpl) {
|
||||||
return !!config.force_refresh[tpl];
|
return !!config.force_refresh[tpl];
|
||||||
};
|
};
|
||||||
|
|
||||||
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) {
|
|
||||||
return $.inArray(tpl, available_templates) !== -1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.prepare = function (raw_tpl) {
|
templates.is_available = function(tpl) {
|
||||||
|
return $.inArray(tpl + '.tpl', available_templates) !== -1;
|
||||||
|
};
|
||||||
|
|
||||||
|
templates.prepare = function(raw_tpl) {
|
||||||
var template = {};
|
var template = {};
|
||||||
template.html = raw_tpl;
|
template.html = raw_tpl;
|
||||||
template.parse = parse;
|
template.parse = parse;
|
||||||
@@ -56,7 +56,7 @@
|
|||||||
.replace('.' + options.settings['view engine'], '');
|
.replace('.' + options.settings['view engine'], '');
|
||||||
|
|
||||||
if (!templates[tpl]) {
|
if (!templates[tpl]) {
|
||||||
fs.readFile(filename, function (err, html) {
|
fs.readFile(filename, function(err, html) {
|
||||||
templates[tpl] = templates.prepare(html.toString());
|
templates[tpl] = templates.prepare(html.toString());
|
||||||
|
|
||||||
return fn(err, templates[tpl].parse(options));
|
return fn(err, templates[tpl].parse(options));
|
||||||
@@ -66,7 +66,7 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.getTemplateNameFromUrl = function (url) {
|
templates.getTemplateNameFromUrl = function(url) {
|
||||||
var parts = url.split('?')[0].split('/');
|
var parts = url.split('?')[0].split('/');
|
||||||
|
|
||||||
for (var i = 0; i < parts.length; ++i) {
|
for (var i = 0; i < parts.length; ++i) {
|
||||||
@@ -86,9 +86,9 @@
|
|||||||
// should be named something else
|
// should be named something else
|
||||||
// TODO: The "Date.now()" in the line below is only there for development purposes.
|
// TODO: The "Date.now()" in the line below is only there for development purposes.
|
||||||
// It should be removed at some point.
|
// It should be removed at some point.
|
||||||
$.get(RELATIVE_PATH + '/templates/' + tpl_name + '.tpl?v=' + Date.now(), function (html) {
|
$.get(RELATIVE_PATH + '/templates/' + tpl_name + '.tpl?v=' + Date.now(), function(html) {
|
||||||
var template = function () {
|
var template = function() {
|
||||||
this.toString = function () {
|
this.toString = function() {
|
||||||
return this.html;
|
return this.html;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -103,7 +103,7 @@
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.load_template = function (callback, url, template) {
|
templates.load_template = function(callback, url, template) {
|
||||||
var location = document.location || window.location,
|
var location = document.location || window.location,
|
||||||
api_url = (url === '' || url === '/') ? 'home' : url,
|
api_url = (url === '' || url === '/') ? 'home' : url,
|
||||||
tpl_url = templates.get_custom_map(api_url.split('?')[0]);
|
tpl_url = templates.get_custom_map(api_url.split('?')[0]);
|
||||||
@@ -125,7 +125,7 @@
|
|||||||
apiXHR = $.ajax({
|
apiXHR = $.ajax({
|
||||||
url: RELATIVE_PATH + '/api/' + api_url,
|
url: RELATIVE_PATH + '/api/' + api_url,
|
||||||
cache: false,
|
cache: false,
|
||||||
success: function (data) {
|
success: function(data) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
ajaxify.go('404');
|
ajaxify.go('404');
|
||||||
return;
|
return;
|
||||||
@@ -134,7 +134,7 @@
|
|||||||
template_data = data;
|
template_data = data;
|
||||||
parse_template();
|
parse_template();
|
||||||
},
|
},
|
||||||
error: function (data, textStatus) {
|
error: function(data, textStatus) {
|
||||||
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
|
$('#content, #footer').stop(true, true).removeClass('ajaxifying');
|
||||||
if (data && data.status == 404) {
|
if (data && data.status == 404) {
|
||||||
return ajaxify.go('404');
|
return ajaxify.go('404');
|
||||||
@@ -157,7 +157,7 @@
|
|||||||
|
|
||||||
var template = templates[tpl_url].parse(template_data)
|
var template = templates[tpl_url].parse(template_data)
|
||||||
|
|
||||||
translator.translate(template, function (translatedTemplate) {
|
translator.translate(template, function(translatedTemplate) {
|
||||||
$('#content').html(translatedTemplate);
|
$('#content').html(translatedTemplate);
|
||||||
|
|
||||||
templates.parseTemplateVariables();
|
templates.parseTemplateVariables();
|
||||||
@@ -170,14 +170,14 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
templates.parseTemplateVariables = function() {
|
templates.parseTemplateVariables = function() {
|
||||||
$('#content [template-variable]').each(function (index, element) {
|
$('#content [template-variable]').each(function(index, element) {
|
||||||
var value = null;
|
var value = null;
|
||||||
|
|
||||||
switch ($(element).attr('template-type')) {
|
switch ($(element).attr('template-type')) {
|
||||||
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;
|
||||||
@@ -196,15 +196,15 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.flush = function () {
|
templates.flush = function() {
|
||||||
parsed_variables = {};
|
parsed_variables = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.get = function (key) {
|
templates.get = function(key) {
|
||||||
return parsed_variables[key];
|
return parsed_variables[key];
|
||||||
};
|
};
|
||||||
|
|
||||||
templates.set = function (key, value) {
|
templates.set = function(key, value) {
|
||||||
parsed_variables[key] = value;
|
parsed_variables[key] = value;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
//modified from https://github.com/psychobunny/dcp.templates
|
//modified from https://github.com/psychobunny/dcp.templates
|
||||||
var parse = function (data) {
|
var parse = function(data) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
function replace(key, value, template) {
|
function replace(key, value, template) {
|
||||||
@@ -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())
|
||||||
return self.indexOf(value) === index;
|
.filter(function(value, index, self) {
|
||||||
}));
|
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;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user