diff options
Diffstat (limited to 'lib/template.js')
-rw-r--r-- | lib/template.js | 31 |
1 files changed, 23 insertions, 8 deletions
diff --git a/lib/template.js b/lib/template.js index 29ce0a9..91b4850 100644 --- a/lib/template.js +++ b/lib/template.js @@ -87,18 +87,21 @@ TemplateEngine.prototype.processBlock = function(blk) { if (_.isString(blk)) blk = { body: blk }; blk = _.defaults(blk, { - parse: false + parse: false, + post: undefined }); blk.id = _.uniqueId("blk"); + var toAdd = blk.parse || (blk.post != undefined); + + // Add to global map + if (toAdd) this.blocks[blk.id] = blk; + //Parsable block, just return it if (blk.parse) { return blk.body; } - // Add to global map - this.blocks[blk.id] = blk; - // Return it as a macro return "%+%"+blk.id+"%+%"; }; @@ -114,9 +117,6 @@ TemplateEngine.prototype.replaceBlocks = function(content) { var body = blk.body; - // Remove it from map - delete that.blocks[key]; - return body; }); }; @@ -363,8 +363,23 @@ TemplateEngine.prototype.renderPage = function(page) { // Post process content TemplateEngine.prototype.postProcess = function(content) { + var that = this; + return Q(content) - .then(this.replaceBlocks); + .then(that.replaceBlocks) + .then(function(content) { + return Q.all(_.map(that.blocks, function(blk, blkId) { + return Q() + .then(function() { + if (!blk.post) return Q(); + return blk.post(); + }) + .then(function() { + delete that.blocks[blkId]; + }); + })) + .thenResolve(content); + }); }; module.exports = TemplateEngine; |