Skip to content

Instantly share code, notes, and snippets.

@acstll
Last active May 28, 2016 07:09
Show Gist options
  • Save acstll/5023658 to your computer and use it in GitHub Desktop.
Save acstll/5023658 to your computer and use it in GitHub Desktop.
Use lodash templates with Express 3
'use strict';
var fs = require('fs');
var _ = require('lodash');
var cache = Object.create(null);
exports.renderFile = function (path, options, callback) {
var html;
var template;
var context = options || {};
if (cache[path]) {
html = cache[path](context);
return callback(null, html);
}
fs.readFile(path, 'utf8', function (err, data) {
if (err) return callback(err);
try {
template = _.template(data);
} catch(err) {
return callback(err);
}
cache[path] = template;
html = template(context);
callback(null, html);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment