Skip to content

Commit

Permalink
Merge pull request #86 from mrmaloof/master
Browse files Browse the repository at this point in the history
Add limit support to <list>.
  • Loading branch information
racke committed Mar 13, 2015
2 parents 4a9e51f + ef9d80e commit 750699b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -114,6 +114,7 @@ t/iterators/paginator.t
t/lists/append.t
t/lists/global-containers.t
t/lists/increments.t
t/lists/limit.t
t/lists/nested.t
t/lists/object.t
t/lists/paging.t
Expand Down
45 changes: 45 additions & 0 deletions lib/Template/Flute.pm
Expand Up @@ -713,6 +713,9 @@ sub _sub_process {
while (my $record_values = $iter_records->next) {

$count_iterations++;
last
if defined $list->{limit}
&& $count_iterations > $list->{limit};

# cut the separators away before copying
for my $sep (@{$list->{separators}}) {
Expand Down Expand Up @@ -2043,6 +2046,48 @@ Output:
</html>
=head3 Limit lists
Sometimes you may wish to limit the number or iterations through you list.
Specification:
<specification>
<list name="images" iterator="images" limit="1">
<param name="image" target="src" field="image_url" />
</list>
</specification>
Template:
<div class="images">
<img class="image" src="/images/bottle.jpg" />
</div>
Code:
$images = [
{ image_url => '/images/bottle1.jpg' },
{ image_url => '/images/bottle2.jpg' },
{ image_url => '/images/bottle3.jpg' },
];
$flute = Template::Flute->new(
template => $html,
specification => $spec,
values => { images => $images },
);
$out = $flute->process;
Output:
<html><head></head><body>
<div class="images">
<img class="image" src="/images/bottle1.jpg" />
</div>
</body></html>
=head1 LISTS
Lists can be accessed after parsing the specification and the HTML template
Expand Down
3 changes: 2 additions & 1 deletion lib/Template/Flute/List.pm
Expand Up @@ -28,14 +28,15 @@ sub new {
if (exists $sob->{iterator}) {
$self->{iterator} = {name => $sob->{iterator}};
}
$self->{limit} = $sob->{limit} if defined $sob->{limit};

bless $self;

if ($spec && $name) {
$self->inputs_add($spec->list_inputs($name));
$self->filters_add($spec->list_filters($name));
$self->sorts_add($spec->list_sorts($name));

if ($lf = $spec->list_paging($name)) {
$self->paging_add($lf);
}
Expand Down
54 changes: 54 additions & 0 deletions t/lists/limit.t
@@ -0,0 +1,54 @@
#
# Tests for list limits
#
use strict;
use warnings;

use Test::More tests => 5;
use Template::Flute;

my ( $spec, $html, $flute, $out, $images );

$spec = q{<specification>
<list name="images" iterator="images">
<param name="image" target="src" field="image_url" />
</list>
</specification>
};

$html = q{<div class="images">
<img class="image" src="/images/bottle.jpg" />
</div>
};

$images = [
{ image_url => '/images/bottle1.jpg' },
{ image_url => '/images/bottle2.jpg' },
{ image_url => '/images/bottle3.jpg' },
];

$flute = Template::Flute->new(
template => $html,
specification => $spec,
values => { images => $images },
);

$out = $flute->process;
like( $out, qr{/images/bottle3.jpg}, q{list no limit} );
unlike( $out, qr{/images/bottle.jpg}, q{list no limit} );

$spec = q{<specification>
<list name="images" iterator="images" limit="1">
<param name="image" target="src" field="image_url" />
</list>
</specification>
};
$flute = Template::Flute->new(
template => $html,
specification => $spec,
values => { images => $images },
);
$out = $flute->process;
like( $out, qr{/images/bottle1.jpg}, q{list with limit} );
unlike( $out, qr{/images/bottle.jpg}, q{list with limit} );
unlike( $out, qr{/images/bottle2.jpg}, q{list with limit} );

0 comments on commit 750699b

Please sign in to comment.