Enable extension "do"

This commit is contained in:
Samy Pessé
2016-04-25 15:44:20 +02:00
parent 0c2dc06cb7
commit f5df41c75f
2 changed files with 37 additions and 2 deletions
+30
View File
@@ -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;
+7 -2
View File
@@ -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()
}
});
}