From 3b235b9acb62d0e29713af1c0d4b23e8b6735990 Mon Sep 17 00:00:00 2001 From: Fabian Becker Date: Fri, 18 Dec 2015 15:06:37 +0100 Subject: [PATCH] Fix broken @last for foreach with limit refs #6205 --- core/server/helpers/foreach.js | 4 ++-- core/test/unit/server_helpers/foreach_spec.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/core/server/helpers/foreach.js b/core/server/helpers/foreach.js index 20eb6f9140..839c0b3a75 100644 --- a/core/server/helpers/foreach.js +++ b/core/server/helpers/foreach.js @@ -18,7 +18,7 @@ foreach = function (context, options) { inverse = options.inverse, columns = options.hash.columns, length = _.size(context), - limit = options.hash.limit || length, + limit = parseInt(options.hash.limit, 10) || length, output = '', data, contextPath; @@ -63,7 +63,7 @@ foreach = function (context, options) { _.each(context, function (item, key) { if (count <= limit) { - execIteration(key, count - 1, count === length); + execIteration(key, count - 1, count === limit); } count += 1; }); diff --git a/core/test/unit/server_helpers/foreach_spec.js b/core/test/unit/server_helpers/foreach_spec.js index 26329a77ce..898032704f 100644 --- a/core/test/unit/server_helpers/foreach_spec.js +++ b/core/test/unit/server_helpers/foreach_spec.js @@ -355,6 +355,14 @@ describe('{{#foreach}} helper', function () { shouldCompileToExpected(templateString, objectHash2, expected); }); + it('@last in foreach with limit', function () { + var templateString = '{{#foreach goodbyes limit="2"}}{{#if @last}}{{text}}! {{/if}}{{/foreach}}cruel {{world}}!', + expected = 'Goodbye! cruel world!'; + + shouldCompileToExpected(templateString, arrayHash2, expected); + shouldCompileToExpected(templateString, objectHash2, expected); + }); + it('foreach with limit 1', function () { var templateString = '', expected = '';