From 57f3e5cd13f87e584b57bb84654f30895361f7f2 Mon Sep 17 00:00:00 2001 From: Rob Brackett Date: Thu, 16 Apr 2015 21:42:28 -0700 Subject: [PATCH] Allow implicit iteration over the root context element. Fixes #154, matches mustache.js behavior. --- lib/template.js | 2 +- test/index.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/template.js b/lib/template.js index 14f396d..373ba59 100644 --- a/lib/template.js +++ b/lib/template.js @@ -141,7 +141,7 @@ var Hogan = {}; doModelGet = this.options.modelGet, cx = null; - if (key === '.' && isArray(ctx[ctx.length - 2])) { + if (key === '.' && (ctx.length === 1 || isArray(ctx[ctx.length - 2]))) { val = ctx[ctx.length - 1]; } else { for (var i = 1; i < names.length; i++) { diff --git a/test/index.js b/test/index.js index ec0777e..82c2e6f 100644 --- a/test/index.js +++ b/test/index.js @@ -691,6 +691,13 @@ test("Implicit Iterator", function() { is(s, " 42 43 44 ", "implicit iterators work"); }); +test("Root-level Implicit Iterator", function() { + var text = '{{#.}} {{name}} {{/.}}'; + var t = Hogan.compile(text); + var s = t.render([{name:"a"},{name:"b"}]); + is(s, " a b ", "root-level implicit iterators work"); +}); + test("Partials And Delimiters", function() { var text = '{{>include}}*\n{{= | | =}}\n*|>include|'; var partialText = ' .{{value}}. ';