Skip to content

Commit

Permalink
Merge pull request #5 from Corion/for-limit-modified
Browse files Browse the repository at this point in the history
For limit modified
  • Loading branch information
sanko committed Feb 13, 2020
2 parents 1c8ef23 + 61a7e71 commit 8458acd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Template/Liquid/Tag/For.pm
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ sub render {
? $limit + (defined $offset ? $offset : 0) - 1
: $#$list);
$max = $#$list if $max > $#$list;
@$list = @{$list}[$min .. $max];
$list = [@{$list}[$min .. $max]]; # make a copy so we can use the list again
@$list = reverse @$list if $reversed;
$limit = defined $limit ? $limit : scalar @$list;
$offset = defined $offset ? $offset : 0;
Expand Down
29 changes: 29 additions & 0 deletions t/0200_tags/02001_for_limit.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use strict;
use warnings;
use lib qw[../../lib ../../blib/lib];
use Test::More; # Requires 0.94 as noted in Build.PL
use Template::Liquid;
use Data::Dumper;
plan tests => 2;

my $template = Template::Liquid->parse(<<'TEMPLATE');
First: {% for post in posts limit:1 %}{{ post }}{% endfor %}
---
{% for post in posts limit:10 %}{{post}},{% endfor %}
TEMPLATE

my @list = qw( 1 2 3 4 5 6 7 8 9 10 11 );

my $result = $template->render( posts => \@list );

is $result, << 'RESULT', "We render the expected list";
First: 1
---
1,2,3,4,5,6,7,8,9,10,
RESULT

# This is a half-truth. Of course, Template::Liquid is allowed to modify the
# list passed in, but it should still contain the original number of elements
# so other template parts can still access that.
is 0+@list, 11, "The original list remains unmodified"
or diag Dumper \@list;

0 comments on commit 8458acd

Please sign in to comment.