blob: 8a725ded4aeb9b5dd92f39c7b875e5e9ee12f790 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var LiquidEngine = require('liquid-node').Engine;
var liquid = new LiquidEngine();
/**
* Manages templates. Only supports Mustache currently/
*/
function OKTemplateRepo(options) {
options = options || {};
this._root = options.root || 'www';
this._ext = options.ext || '.mustache';
this._cache = {};
this._populateCache(this._cache);
}
OKTemplateRepo.prototype.getTemplate = function getTemplate(name) {
return this._cache[name];
}
/**
* Go through our template dir and read the template files
* into memory as strings.
* Assumes all templates fit into memory.
*/
OKTemplateRepo.prototype._populateCache = function _populateCache(cache) {
var self = this;
var files = glob.sync(this._root + '/*' + this._ext);
files.forEach(function eachFile(file) {
var name = path.basename(file, self._ext);
var templateString = fs.readFileSync(file, {encoding: 'UTF8'});
cache[name] = {
name: name,
templateString: templateString,
render: function(data) {
// TODO Not sure if this caches parsed templates behind the scenes?
return liquid.parseAndRender(templateString, data);
}
}
});
}
module.exports = OKTemplateRepo;
|