mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
allow express to serve parsed tpls via res.render
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
var config = {},
|
||||
templates,
|
||||
fs = null,
|
||||
path = null,
|
||||
available_templates = [],
|
||||
parsed_variables = {},
|
||||
apiXHR;
|
||||
@@ -12,7 +13,8 @@
|
||||
};
|
||||
|
||||
try {
|
||||
fs = require('fs');
|
||||
fs = require('fs'),
|
||||
path = require('path');
|
||||
} catch (e) {}
|
||||
|
||||
templates.force_refresh = function (tpl) {
|
||||
@@ -127,6 +129,27 @@
|
||||
loadTemplates(templates_to_load || [], custom_templates || false);
|
||||
}
|
||||
|
||||
templates.render = function(filename, options, fn) {
|
||||
if ('function' === typeof options) {
|
||||
fn = options, options = false;
|
||||
}
|
||||
|
||||
var tpl = filename
|
||||
.replace(path.join(__dirname + '/../templates/'), '')
|
||||
.replace('.' + options.settings['view engine'], '');
|
||||
|
||||
if (!templates[tpl]) {
|
||||
fs.readFile(filename, function (err, html) {
|
||||
templates[tpl] = html.toString();
|
||||
templates.prepare(templates[tpl]);
|
||||
|
||||
return fn(err, templates[tpl].parse(options));
|
||||
});
|
||||
} else {
|
||||
return fn(null, templates[tpl].parse(options));
|
||||
}
|
||||
}
|
||||
|
||||
templates.getTemplateNameFromUrl = function (url) {
|
||||
var parts = url.split('?')[0].split('/');
|
||||
|
||||
@@ -419,6 +442,8 @@
|
||||
})(data, "", template);
|
||||
}
|
||||
|
||||
module.exports.__express = module.exports.render;
|
||||
|
||||
if ('undefined' !== typeof window) {
|
||||
window.templates = module.exports;
|
||||
templates.init();
|
||||
|
||||
@@ -226,6 +226,10 @@ process.on('uncaughtException', function(err) {
|
||||
|
||||
// Middlewares
|
||||
app.configure(function() {
|
||||
app.engine('tpl', templates.__express);
|
||||
app.set('view engine', 'tpl');
|
||||
app.set('views', path.join(__dirname, '../public/templates'));
|
||||
|
||||
async.series([
|
||||
function(next) {
|
||||
// Pre-router middlewares
|
||||
|
||||
Reference in New Issue
Block a user