Files
bower/lib/util/template.js
Chris Aniszczyk c220329337 Initial Release
Signed-off-by: Chris Aniszczyk <zx@twitter.com>
2012-09-06 17:11:42 -07:00

39 lines
1.2 KiB
JavaScript

// ==========================================
// BOWER: Hogan Renderer w/ template cache
// ==========================================
// Copyright 2012 Twitter, Inc
// Licensed under The MIT License
// http://opensource.org/licenses/MIT
// ==========================================
var events = require('events');
var hogan = require('hogan.js');
var path = require('path');
var fs = require('fs');
var colors = require('../util/hogan-colors');
var templates = {};
module.exports = function (name, context, sync) {
var emitter = new events.EventEmitter;
var templateName = name + '.mustache';
var templatePath = path.join(__dirname, '../../templates/', templateName);
if (sync) {
if (!templates[templatePath]) templates[templatePath] = fs.readFileSync(templatePath, 'utf-8');
return hogan.compile(templates[templatePath]).renderWithColors(context);
} else if (templates[templatePath]) {
process.nextTick(function () {
emitter.emit('data', hogan.compile(templates[templatePath]).renderWithColors(context));
});
} else {
fs.readFile(templatePath, 'utf-8', function (err, file) {
templates[templatePath] = file;
emitter.emit('data', hogan.compile(file).renderWithColors(context));
});
}
return emitter;
};