summaryrefslogtreecommitdiff
path: root/app/node_modules/oktemplate/index.js
diff options
context:
space:
mode:
authorSean Fridman <fridman@mail.sfsu.edu>2015-06-05 15:01:01 -0400
committerSean Fridman <fridman@mail.sfsu.edu>2015-06-05 15:02:40 -0400
commit5082f3cad3f8f730ab4cc21f6b6cdf05c37436dd (patch)
tree7c774265ceb8e6732a685ff315395c678c4ad700 /app/node_modules/oktemplate/index.js
parent045bf1936bedf5c56a0f983b242c4f53f6766cf6 (diff)
Implement template reloadingv0.1.9
Diffstat (limited to 'app/node_modules/oktemplate/index.js')
-rw-r--r--app/node_modules/oktemplate/index.js59
1 files changed, 42 insertions, 17 deletions
diff --git a/app/node_modules/oktemplate/index.js b/app/node_modules/oktemplate/index.js
index f2c2e07..ae9d1b1 100644
--- a/app/node_modules/oktemplate/index.js
+++ b/app/node_modules/oktemplate/index.js
@@ -4,6 +4,7 @@ var path = require('path');
var glob = require('glob');
var stringify = require('json-to-html');
var Liquid = require('liquid-node');
+var chokidar = require('chokidar');
/**
* Define any custom liquid filters here.
@@ -40,19 +41,56 @@ var filters = {
*/
function OKTemplateRepo(options) {
options = options || {};
+ var self = this;
var root = this._root = options.root || 'templates';
- var ext = 'liquid';
+ // TODO Support more templates?
+ var ext = this._ext = 'liquid';
var cache = this._cache = {};
var engine = this._engine = new Liquid.Engine;
+ var debug = options.debug;
+ var globString = this._globString = root + '/*.' + ext
engine.registerFilters(filters);
engine.fileSystem = new Liquid.LocalFileSystem(root, ext);
+
this._populateCache(engine, cache, ext);
+
+ if (debug) {
+ var watcher = chokidar.watch(globString);
+ watcher.on('change', reloadTemplate);
+ watcher.on('add', reloadTemplate);
+ }
+
+ function reloadTemplate(path) {
+ self._loadTemplate(path);
+ }
}
OKTemplateRepo.prototype.getTemplate = function getTemplate(name) {
return this._cache[name];
}
+OKTemplateRepo.prototype._loadTemplate = function loadTemplate(filePath) {
+ var engine = this._engine
+ var name = path.basename(filePath, '.' + this._ext);
+ var templateString = fs.readFileSync(filePath, {encoding: 'UTF8'});
+ if (!this._cache[name])
+ this._cache[name] = {};
+
+ var template = this._cache[name]
+ template.name = name;
+ template.templateString = templateString;
+ template.render = render
+
+ function render(data) {
+ return Q.promise(function(resolve, reject) {
+ // TODO Not sure if this caches parsed templates behind the scenes?
+ engine.parseAndRender(templateString, data)
+ .then(resolve)
+ .catch(reject);
+ });
+ }
+}
+
/**
* Go through our template dir and read the template files
* into memory as strings.
@@ -60,22 +98,9 @@ OKTemplateRepo.prototype.getTemplate = function getTemplate(name) {
*/
OKTemplateRepo.prototype._populateCache = function _populateCache(engine, cache, ext) {
var self = this;
- var files = glob.sync(this._root + '/*.' + ext);
- files.forEach(function eachFile(file) {
- var name = path.basename(file, '.' + ext);
- var templateString = fs.readFileSync(file, {encoding: 'UTF8'});
- cache[name] = {
- name: name,
- templateString: templateString,
- render: function(data) {
- return Q.promise(function(resolve, reject) {
- // TODO Not sure if this caches parsed templates behind the scenes?
- engine.parseAndRender(templateString, data)
- .then(resolve)
- .catch(reject);
- });
- }
- }
+ var files = glob.sync(this._globString);
+ files.forEach(function eachFile(path) {
+ self._loadTemplate(path);
});
}