summaryrefslogtreecommitdiffstats
path: root/lib/models/templateBlock.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/models/templateBlock.js')
-rw-r--r--lib/models/templateBlock.js54
1 files changed, 12 insertions, 42 deletions
diff --git a/lib/models/templateBlock.js b/lib/models/templateBlock.js
index 2ec1328..200e048 100644
--- a/lib/models/templateBlock.js
+++ b/lib/models/templateBlock.js
@@ -8,8 +8,6 @@ var TemplateShortcut = require('./templateShortcut');
var NODE_ENDARGS = '%%endargs%%';
-var blockBodies = {};
-
var TemplateBlock = Immutable.Record({
// Name of block, also the start tag
name: String(),
@@ -26,9 +24,6 @@ var TemplateBlock = Immutable.Record({
// List of shortcuts to replace with this block
shortcuts: Immutable.Map(),
- // Function to execute in post processing
- post: null,
-
parse: true
}, 'TemplateBlock');
@@ -36,10 +31,6 @@ TemplateBlock.prototype.getName = function() {
return this.get('name');
};
-TemplateBlock.prototype.getPost = function() {
- return this.get('post');
-};
-
TemplateBlock.prototype.getParse = function() {
return this.get('parse');
};
@@ -85,7 +76,7 @@ TemplateBlock.prototype.getExtensionName = function() {
@return {Nunjucks.Extension}
*/
-TemplateBlock.prototype.toNunjucksExt = function(mainContext) {
+TemplateBlock.prototype.toNunjucksExt = function(mainContext, blocksOutput) {
var that = this;
var name = this.getName();
var endTag = this.getEndTag();
@@ -195,7 +186,7 @@ TemplateBlock.prototype.toNunjucksExt = function(mainContext) {
return that.applyBlock(mainBlock, ctx);
})
.then(function(result) {
- return that.blockResultToHtml(result);
+ return that.blockResultToHtml(result, blocksOutput);
})
.nodeify(callback);
};
@@ -221,19 +212,19 @@ TemplateBlock.prototype.applyBlock = function(inner, context) {
var r = processFn.call(context, inner);
if (Promise.isPromiseAlike(r)) {
- return r.then(this.handleBlockResult.bind(this));
+ return r.then(this.normalizeBlockResult.bind(this));
} else {
- return this.handleBlockResult(r);
+ return this.normalizeBlockResult(r);
}
};
/**
- Handle result from a block process function
+ Normalize result from a block process function
- @param {Object} result
+ @param {Object|String} result
@return {Object}
*/
-TemplateBlock.prototype.handleBlockResult = function(result) {
+TemplateBlock.prototype.normalizeBlockResult = function(result) {
if (is.string(result)) {
result = { body: result };
}
@@ -246,15 +237,17 @@ TemplateBlock.prototype.handleBlockResult = function(result) {
Convert a block result to HTML
@param {Object} result
+ @param {Object} blocksOutput: stored post processing blocks in this object
@return {String}
*/
-TemplateBlock.prototype.blockResultToHtml = function(result) {
+TemplateBlock.prototype.blockResultToHtml = function(result, blocksOutput) {
var parse = this.getParse();
var indexedKey;
- var toIndex = (!parse) || (this.getPost() !== undefined);
+ var toIndex = (!parse) || (result.post !== undefined);
if (toIndex) {
- indexedKey = TemplateBlock.indexBlockResult(result);
+ indexedKey = genKey();
+ blocksOutput[indexedKey] = result;
}
// Parsable block, just return it
@@ -268,29 +261,6 @@ TemplateBlock.prototype.blockResultToHtml = function(result) {
};
/**
- Index a block result, and return the indexed key
-
- @param {Object} blk
- @return {String}
-*/
-TemplateBlock.indexBlockResult = function(blk) {
- var key = genKey();
- blockBodies[key] = blk;
-
- return key;
-};
-
-/**
- Get a block results indexed for a specific key
-
- @param {String} key
- @return {Object|undefined}
-*/
-TemplateBlock.getBlockResultByKey = function(key) {
- return blockBodies[key];
-};
-
-/**
Create a template block from a function or an object
@param {String} blockName