diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/models/templateEngine.js | 30 | ||||
-rw-r--r-- | lib/output/website/createTemplateEngine.js | 9 |
2 files changed, 37 insertions, 2 deletions
diff --git a/lib/models/templateEngine.js b/lib/models/templateEngine.js index 28322ea..114aa73 100644 --- a/lib/models/templateEngine.js +++ b/lib/models/templateEngine.js @@ -5,6 +5,9 @@ var TemplateEngine = Immutable.Record({ // List of {TemplateBlock} blocks: Immutable.List(), + // List of Extension + extensions: Immutable.Map(), + // Map of filters: {String} name -> {Function} fn filters: Immutable.Map(), @@ -42,6 +45,10 @@ TemplateEngine.prototype.getContext = function() { return this.get('context'); }; +TemplateEngine.prototype.getExtensions = function() { + return this.get('extensions'); +}; + /** Return a block by its name (or undefined) @@ -66,6 +73,7 @@ TemplateEngine.prototype.toNunjucks = function() { var blocks = this.getBlocks(); var filters = this.getFilters(); var globals = this.getGlobals(); + var extensions = this.getExtensions(); var env = new nunjucks.Environment( loader, @@ -103,6 +111,11 @@ TemplateEngine.prototype.toNunjucks = function() { env.addGlobal(globalName, globalValue); }); + // Add other extensions + extensions.forEach(function(ext, extName) { + env.addExtension(extName, ext); + }); + return env; }; @@ -116,4 +129,21 @@ TemplateEngine.prototype.bindToContext = function(fn) { return fn.bind(this.getContext()); }; +/** + Create a template engine + + @param {Object} def + @return {TemplateEngine} +*/ +TemplateEngine.create = function(def) { + return new TemplateEngine({ + blocks: Immutable.List(def.blocks || []), + extensions: Immutable.Map(def.extensions || {}), + filters: Immutable.Map(def.filters || {}), + globals: Immutable.Map(def.globals || {}), + context: def.context, + loader: def.loader + }); +}; + module.exports = TemplateEngine; diff --git a/lib/output/website/createTemplateEngine.js b/lib/output/website/createTemplateEngine.js index b90c3ad..5c5acee 100644 --- a/lib/output/website/createTemplateEngine.js +++ b/lib/output/website/createTemplateEngine.js @@ -1,4 +1,6 @@ var path = require('path'); +var nunjucks = require('nunjucks'); +var DoExtension = require('nunjucks-do')(nunjucks); var TEMPLATES_FOLDER = require('../../constants/templatesFolder'); @@ -27,8 +29,11 @@ function createTemplateEngine(output) { var loader = new Templating.ThemesLoader(tplSearchPaths); - return new TemplateEngine({ - loader: loader + return TemplateEngine.create({ + loader: loader, + extensions: { + 'DoExtension': new DoExtension() + } }); } |