diff --git a/index.js b/index.js index 9dcf0326..7acb6473 100644 --- a/index.js +++ b/index.js @@ -39,9 +39,9 @@ function TemplateError(error) { }}; } -function createRenderFunc (code) { +function createRenderFunc(code) { // The compiled render function - this is all we need - return new Function('_context', '_parents', '_filters', '_', '_ext', [ + return new Function('_context', '_parents', '_filters', '_', '_ext', [ '_parents = _parents ? _parents.slice() : [];', '_context = _context || {};', // Prevents circular includes (which will crash node without warning) @@ -94,15 +94,13 @@ function createTemplate(data, id) { if (code !== false) { render = createRenderFunc(code); - } - - else { + } else { render = function (_context, _parents, _filters, _, _ext) { template.tokens = tokens; code = parser.compile.call(template, null, '', _context); var fn = createRenderFunc(code); return fn.call(this, _context, _parents, _filters, _, _ext); - } + }; } template.render = function (context, parents) { diff --git a/index.test.js b/index.test.js index 72dae977..a0701563 100644 --- a/index.test.js +++ b/index.test.js @@ -84,8 +84,8 @@ exports.compileFile = testCase({ r3 = tpl.render({baseTmpl: "extends_base.html"}); r4 = tpl.render({baseTmpl: "extends_base2.html"}); - test.strictEqual(r1,r3, "this should not throw"); - test.strictEqual(r2,r4, "this should not throw"); + test.strictEqual(r1, r3, "rendering the same template with the same context twice, should return identically."); + test.strictEqual(r2, r4, "rendering the same template with the same context twice, should return identically."); test.notEqual(r1, r2, "these should not be equal, as they use different base templates."); diff --git a/lib/parser.js b/lib/parser.js index 4776e89a..01366908 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -31,7 +31,7 @@ function getArgs(input) { function getContextVar(varName, context) { var a = varName.split("."); while (a.length) { - context = context[a.splice(0,1)[0]]; + context = context[a.splice(0, 1)[0]]; } return context; } @@ -325,16 +325,14 @@ exports.compile = function compile(indent, parentBlock, context) { // Load the parent template if (token.name === 'extends') { filepath = token.args[0]; - + if (!helpers.isStringLiteral(filepath)) { if (!context) { extendsHasVar = true; return; } - else { - filepath = "\"" + getContextVar(filepath, context) + "\""; - } + filepath = "\"" + getContextVar(filepath, context) + "\""; } if (!helpers.isStringLiteral(filepath) || token.args.length > 1) {