summaryrefslogtreecommitdiffstats
path: root/lib/models
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2016-04-25 15:44:20 +0200
committerSamy Pessé <samypesse@gmail.com>2016-04-25 15:44:20 +0200
commitf5df41c75f6e6d25ffc9f86feeac29892fb9f856 (patch)
treed51e266f53ac17c4245673d0c7ca9535501297e4 /lib/models
parent0c2dc06cb721a437480d5a10611511b8f84b00a8 (diff)
downloadgitbook-f5df41c75f6e6d25ffc9f86feeac29892fb9f856.zip
gitbook-f5df41c75f6e6d25ffc9f86feeac29892fb9f856.tar.gz
gitbook-f5df41c75f6e6d25ffc9f86feeac29892fb9f856.tar.bz2
Enable extension "do"
Diffstat (limited to 'lib/models')
-rw-r--r--lib/models/templateEngine.js30
1 files changed, 30 insertions, 0 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;