Skip to content

Commit

Permalink
Merge pull request TryGhost#6242 from halfdan/6205-last-limit
Browse files Browse the repository at this point in the history
Fix broken @last for foreach with limit
  • Loading branch information
ErisDS committed Dec 20, 2015
2 parents fe91944 + 3b235b9 commit 9683492
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/server/helpers/foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
});
Expand Down
8 changes: 8 additions & 0 deletions core/test/unit/server_helpers/foreach_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<ul>{{#foreach posts limit="1"}}<li>{{title}}</li>{{else}}not this{{/foreach}}</ul>',
expected = '<ul><li>first</li></ul>';
Expand Down

0 comments on commit 9683492

Please sign in to comment.