Remove used blocks from templates

This commit is contained in:
Samy Pessé
2015-01-29 14:18:13 +01:00
parent 2bec7c5e10
commit 9172948c18
+16 -6
View File
@@ -80,18 +80,22 @@ var TemplateEngine = function(book) {
// Process a block in a context
TemplateEngine.prototype.processBlock = function(blk) {
if (_.isString(blk)) blk = { body: blk };
blk = _.defaults(blk, {
parse: false
});
blk.id = _.uniqueId("blk");
//Parsable block, just return it
if (blk.parse) {
return blk.body;
}
// Add to global map
this.blocks[blk.id] = blk;
// If don't parse id, return it as a macro
if (!blk.parse) return "%+%"+blk.id+"%+%";
return blk.body;
// Return it as a macro
return "%+%"+blk.id+"%+%";
};
// Replace blocks by body after processing
@@ -101,8 +105,14 @@ TemplateEngine.prototype.replaceBlocks = function(content) {
return content.replace(/\%\+\%([\s\S]+?)\%\+\%/g, function(match, key) {
var blk = that.blocks[key];
if (!blk || blk.parse) return match;
return blk.body;
if (!blk) return match;
var body = blk.body;
// Remove it from map
delete that.blocks[key];
return body;
});
};