diff --git a/browser/TemplateProvider.js b/browser/TemplateProvider.js index aab3cc9..77776c6 100644 --- a/browser/TemplateProvider.js +++ b/browser/TemplateProvider.js @@ -39,7 +39,6 @@ module.exports = TemplateProvider; */ function TemplateProvider($nunjucks) { this._nunjucks = $nunjucks; - this._templates = {}; } /** @@ -49,13 +48,6 @@ function TemplateProvider($nunjucks) { */ TemplateProvider.prototype._nunjucks = null; -/** - * Current set of registered templates. - * @type {Object} - * @private - */ -TemplateProvider.prototype._templates = null; - /** * Registers compiled (precompiled) Nunjucks template. * http://mozilla.github.io/nunjucks/api.html @@ -63,7 +55,8 @@ TemplateProvider.prototype._templates = null; * @param {String} compiled Compiled template source. */ TemplateProvider.prototype.registerCompiled = function (name, compiled) { - this._templates[name] = this._nunjucks.compile(compiled); + // jshint evil:true + eval(compiled); }; /** @@ -73,15 +66,14 @@ TemplateProvider.prototype.registerCompiled = function (name, compiled) { * @returns {*} */ TemplateProvider.prototype.render = function (name, data) { - if (!this._templates.hasOwnProperty(name)) { - return Promise.reject(new Error('No such template')); - } - - var promise; - try { - promise = Promise.resolve(this._templates[name].render(data)); - } catch(e) { - promise = Promise.reject(e); - } - return promise; + var self = this; + return new Promise(function (fulfill, reject) { + self._nunjucks.render(name, data, function (error, html) { + if (error) { + reject(error); + return; + } + fulfill(html); + }); + }); }; \ No newline at end of file diff --git a/lib/TemplateProvider.js b/lib/TemplateProvider.js index 7862bce..967c373 100644 --- a/lib/TemplateProvider.js +++ b/lib/TemplateProvider.js @@ -32,15 +32,33 @@ module.exports = TemplateProvider; -var BrowserTemplateProvider = require('../browser/TemplateProvider'), - util = require('util'); - -util.inherits(TemplateProvider, BrowserTemplateProvider); - function TemplateProvider($nunjucks) { - BrowserTemplateProvider.call(this, $nunjucks); + this._nunjucks = $nunjucks; + this._templates = {}; + this._compiled = {}; } +/** + * Current Nunjucks factory. + * @type {Nunjucks} + * @private + */ +TemplateProvider.prototype._nunjucks = null; + +/** + * Current set of registered templates. + * @type {Object} + * @private + */ +TemplateProvider.prototype._templates = null; + +/** + * Current set of compiled templates. + * @type {Object} + * @private + */ +TemplateProvider.prototype._compiled = null; + /** * Compiles (precompiles) Nunjucks template. * http://mozilla.github.io/nunjucks/api.html @@ -48,7 +66,42 @@ function TemplateProvider($nunjucks) { * @returns {String} Precompiled source (template specification). */ TemplateProvider.prototype.compile = function (source, name) { - return this._nunjucks.precompileString(source, { + this._compiled[name] = new this._nunjucks.Template(source); + return this._nunjucks.precompileString(source, { name: name }); +}; + +/** + * Registers compiled (precompiled) Nunjucks template. + * http://mozilla.github.io/nunjucks/api.html + * @param {String} name Template name. + * @param {String} compiled Compiled template source. + */ +TemplateProvider.prototype.registerCompiled = function (name, compiled) { + this._templates[name] = this._compiled[name]; +}; + +/** + * Renders template with specified data. + * @param {String} name Name of template. + * @param {Object} data Data context for template. + * @returns {*} + */ +TemplateProvider.prototype.render = function (name, data) { + var self = this; + return new Promise(function (fulfill, reject) { + if (!self._templates.hasOwnProperty(name)) { + reject(new Error('No such template')); + return; + } + + self._templates[name].render(data, function (error, html) { + if (error) { + reject(error); + return; + } + fulfill(html); + }); + }); }; \ No newline at end of file diff --git a/package.json b/package.json index 2b1578b..7dfcf5e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "contributors": [], "main": "./index.js", "browser": { - "nunjucks": "./node_modules/nunjucks/browser/nunjucks.js", + "nunjucks": "./node_modules/nunjucks/browser/nunjucks-slim.js", "./lib/TemplateProvider.js": "./browser/TemplateProvider.js" }, "repository": {