Skip to content

Commit

Permalink
support lists with variables as index rather than just numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
Amit Gupta committed Nov 20, 2024
1 parent a1144af commit a0067ea
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/Template/Liquid/Context.pm
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ sub get {

# print STDERR "DEBUG:obj property. var=$var. 1=$1,2=$2";
my $obj = $s->get($1);
return $obj->{$2} if $obj;
my $objreftype = ref $obj;
return $obj->{$2} if $objreftype eq "HASH";

if($objreftype eq 'ARRAY') {
# resolve the scope variable into an array index
my $a_idx = $s->get($2);
# print STDERR "\nDEBUG:a_idx=$a_idx";
return @$obj[$a_idx] if @$obj[$a_idx];
}

return; # return if nothing
}
STEP: while (@path) {
Expand Down
6 changes: 6 additions & 0 deletions t/0200_tags/02001_for.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use Template::Liquid;
#{% assign items = "foo"%}{%for item in items%}{{item}}{%endfor%}
$|++;
#
is( Template::Liquid->parse(
<<'TEMPLATE')->render(array => "foo,bar"), <<'EXPECTED', 'string split with index');
{% assign items = array | split: "," %}{% for i in (0..2) %}{{items[i]}}{%endfor%}
TEMPLATE
foobar
EXPECTED
is( Template::Liquid->parse(
<<'TEMPLATE')->render(array => "foo,bar"), <<'EXPECTED', 'string split');
{% assign items = array | split: "," %}{%for item in items%}{{item}}{%endfor%}
Expand Down

0 comments on commit a0067ea

Please sign in to comment.