diff options
-rw-r--r-- | lib/template.js | 44 | ||||
-rw-r--r-- | test/plugins.js | 10 | ||||
-rw-r--r-- | test/plugins/blocks/index.js | 10 |
3 files changed, 51 insertions, 13 deletions
diff --git a/lib/template.js b/lib/template.js index 91cedca..e57ef1b 100644 --- a/lib/template.js +++ b/lib/template.js @@ -103,25 +103,45 @@ TemplateEngine.prototype.addBlock = function(name, block) { this.tags = [name]; this.parse = function(parser, nodes, lexer) { - console.log("parse", name, block.end); - var bodies = {}; + var body = null; + var lastBlockName = null; + var lastBlockArgs = null; + var allBlocks = block.blocks.concat([block.end]); + var subbodies = {}; var tok = parser.nextToken(); var args = parser.parseSignature(null, true); parser.advanceAfterBlockEnd(tok.value); - // parse the body and possibly the error block, which is optional - /*var body = parser.parseUntilBlocks('error', 'endtruncate'); - var errorBody = null; - - if(parser.skipSymbol('error')) { - parser.skip(lexer.TOKEN_BLOCK_END); - errorBody = parser.parseUntilBlocks('endremote'); + console.log("start parsing", allBlocks); + while (1) { + var currentBody = parser.parseUntilBlocks.apply(parser, allBlocks); + + // Handle body with previous block name and args + if (lastBlockName) { + subbodies[lastBlockName] = subbodies[lastBlockName] || []; + subbodies[lastBlockName].push({ + body: currentBody, + args: lastBlockArgs + }); + } else { + body = currentBody; + } + + var tok = parser.peekToken(); + lastBlockName = tok.value; + if (lastBlockName == block.end) { + console.log("finish!"); + break; + } + + lastBlockArgs = parser.parseSignature(null, true); + parser.advanceAfterBlockEnd(lastBlockName); + + console.log(" -> keep going"); } - parser.advanceAfterBlockEnd();*/ - - var body = parser.parseUntilBlocks(block.end); + console.log(body, subbodies); parser.advanceAfterBlockEnd(); return new nodes.CallExtensionAsync(this, 'run', args, [body]); diff --git a/test/plugins.js b/test/plugins.js index 9b9f58e..560a779 100644 --- a/test/plugins.js +++ b/test/plugins.js @@ -107,5 +107,15 @@ describe('Plugins', function () { done ); }); + + it('should correctly extend template blocks with sub-blocks', function(done) { + qdone( + books[0].template.renderString('{% test3join %}hello{% also %}the{% also %}world{% endtest3join %}') + .then(function(content) { + assert.equal(content, "hello the world"); + }), + done + ); + }); }); }); diff --git a/test/plugins/blocks/index.js b/test/plugins/blocks/index.js index 91e8b55..0f5fb87 100644 --- a/test/plugins/blocks/index.js +++ b/test/plugins/blocks/index.js @@ -10,6 +10,14 @@ module.exports = { process: function(args) { return "test2"+args.body+"test2"; } - } + }, + "test3join": { + blocks: [ + "also" + ], + process: function(args) { + return "test"; + } + } } };
\ No newline at end of file |