Skip to content
This repository has been archived by the owner on Jul 11, 2023. It is now read-only.

Commit

Permalink
Fix precompilation problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Rechkunov committed Mar 5, 2015
1 parent 4794a2c commit e7f8aa9
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 28 deletions.
32 changes: 12 additions & 20 deletions browser/TemplateProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ module.exports = TemplateProvider;
*/
function TemplateProvider($nunjucks) {
this._nunjucks = $nunjucks;
this._templates = {};
}

/**
Expand All @@ -49,21 +48,15 @@ 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
* @param {String} name Template name.
* @param {String} compiled Compiled template source.
*/
TemplateProvider.prototype.registerCompiled = function (name, compiled) {
this._templates[name] = this._nunjucks.compile(compiled);
// jshint evil:true
eval(compiled);
};

/**
Expand All @@ -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);
});
});
};
67 changes: 60 additions & 7 deletions lib/TemplateProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,76 @@

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
* @param {String} source Template source.
* @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);
});
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit e7f8aa9

Please sign in to comment.