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