Skip to content

Commit

Permalink
Add support for {% continue %} interrupt in for loops
Browse files Browse the repository at this point in the history
  • Loading branch information
sanko committed Oct 1, 2012
1 parent 8d4947a commit 8ff3d33
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@
Version 1.0.0 | Soon | xxxxxxxxxx

API Changes/Compatibility Information:
* Support {% break %} tag in for loops (see docs)
* Support {% break %} and {% continue %} tags in for loops (see docs)

Notes:
* Project renamed from Solution to Template::Liquid because... oh, who
Expand Down
2 changes: 2 additions & 0 deletions MANIFEST
Expand Up @@ -15,6 +15,7 @@ lib/Template/Liquid/Tag/Break.pm
lib/Template/Liquid/Tag/Capture.pm
lib/Template/Liquid/Tag/Case.pm
lib/Template/Liquid/Tag/Comment.pm
lib/Template/Liquid/Tag/Continue.pm
lib/Template/Liquid/Tag/Cycle.pm
lib/Template/Liquid/Tag/For.pm
lib/Template/Liquid/Tag/If.pm
Expand All @@ -40,4 +41,5 @@ t/0200_tags/02008_include.t
t/0200_tags/02009_assign.t
t/0200_tags/02010_raw.t
t/0200_tags/02011_break.t
t/0200_tags/02012_continue.t
t/0200_tags/_includes/testing.inc
3 changes: 2 additions & 1 deletion lib/Template/Liquid.pm
Expand Up @@ -19,6 +19,7 @@ use Template::Liquid::Tag::Break;
use Template::Liquid::Tag::Capture;
use Template::Liquid::Tag::Case;
use Template::Liquid::Tag::Comment;
use Template::Liquid::Tag::Continue;
use Template::Liquid::Tag::Cycle;
use Template::Liquid::Tag::For;
use Template::Liquid::Tag::If;
Expand All @@ -39,7 +40,7 @@ sub resolve { $_[0]->{'context'}->resolve($_[1], $_[2]) }
#
sub new {
my ($class) = @_;
my $s = bless { break => 0 }, $class;
my $s = bless { break => 0, continue => 0 }, $class;
return $s;
}

Expand Down
65 changes: 65 additions & 0 deletions lib/Template/Liquid/Tag/Continue.pm
@@ -0,0 +1,65 @@
package Template::Liquid::Tag::Continue;
{ $Template::Liquid::Tag::Continue::VERSION = 'v1.0.0' }
use strict;
use warnings;
use lib '../../../lib';
use Template::Liquid::Error;
use Template::Liquid::Utility;
BEGIN { our @ISA = qw[Template::Liquid::Tag::Break]; }
sub import {Template::Liquid::register_tag('continue', __PACKAGE__) }

sub render {
my $s = shift;
$s->template->{continue} = 1;
return '';
}
1;

=pod
=head1 NAME
Template::Liquid::Tag::Continue - For-block jitter construct
=head1 Synopsis
{% for item in collection %}
{% if item.other_condition %}
{% continue %}
{% endif %}
{% endfor %}
=head1 Description
You can use the C<{% continue %}> tag to fall through the current iteration
of the enclosing L<<C<{% for .. %}> |Template::Liquid::Tag::For>> block.
=head1 See Also
Liquid for Designers: http://wiki.github.com/tobi/liquid/liquid-for-designers
L<Template::Liquid|Template::Liquid/"Create your own filters">'s docs on
custom filter creation
=head1 Author
Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/
The original Liquid template system was developed by jadedPixel
(http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/).
=head1 License and Legal
Copyright (C) 2009-2012 by Sanko Robinson E<lt>sanko@cpan.orgE<gt>
This program is free software; you can redistribute it and/or modify it under
the terms of The Artistic License 2.0. See the F<LICENSE> file included with
this distribution or http://www.perlfoundation.org/artistic_license_2_0. For
clarification, see http://www.perlfoundation.org/artistic_2_0_notes.
When separated from the distribution, all original POD documentation is
covered by the Creative Commons Attribution-Share Alike 3.0 License. See
http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For
clarification, see http://creativecommons.org/licenses/by-sa/3.0/us/.
=cut
4 changes: 4 additions & 0 deletions lib/Template/Liquid/Tag/For.pm
Expand Up @@ -143,6 +143,10 @@ sub render {
$s->template->{break} = 0;
last STEP;
}
if ($s->template->{continue}) {
$s->template->{continue} = 0;
next STEP;
}
}
}
return $return;
Expand Down
59 changes: 59 additions & 0 deletions t/0200_tags/02012_continue.t
@@ -0,0 +1,59 @@
use strict;
use warnings;
use lib qw[../../lib ../../blib/lib];
use Test::More; # Requires 0.94 as noted in Build.PL
use Template::Liquid;
#
is( Template::Liquid->parse(
<<'INPUT')->render(), <<'EXPECTED', 'no-op on render');
{% continue %}
INPUT

EXPECTED
#
my $assigns = {'array' => {'items' => [1, 2, 3, 4, 5]}};
is( Template::Liquid->parse(
<<'INPUT')->render($assigns), <<'EXPECTED', 'continue drops out of for loop');
{% for i in array.items %}{% continue %}{% endfor %}
INPUT

EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render($assigns), <<'EXPECTED', 'drop out after first iteration');
{% for i in array.items %}{{ i }}{% continue %}{% endfor %}
INPUT
12345
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render($assigns), <<'EXPECTED', 'skip out of loop before var is rendered');
{% for i in array.items %}{% continue %}{{ i }}{% endfor %}
INPUT

EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render($assigns), <<'EXPECTED', 'skips out of the local for loop and not all of them');
{% for i in array.items %}{% if i > 3 %}{% continue %}{% endif %}{{ i }}{% endfor %}
INPUT
123
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render($assigns), <<'EXPECTED', 'continue does nothing when unreached');
{% for i in array.items %}{% if i == 3 %}{% continue %}{% else %}{{ i }}{% endif %}{% endfor %}
INPUT
1245
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render({array => [[1, 2], [3, 4], [5, 6]]}), <<'EXPECTED', 'ensure it only continues the local for loop and not all of them');
{% for item in array %}{% for i in item %}{% if i == 1 %}{% continue %}{% endif %}{{ i }}{% endfor %}{% endfor %}
INPUT
23456
EXPECTED
is( Template::Liquid->parse(
<<'INPUT')->render({array => {items => [1, 2, 3, 4, 5]}}), <<'EXPECTED', 'continue does nothing when unreached');
{% for i in array.items %}{% if i == 9999 %}{% continue %}{% endif %}{{ i }}{% endfor %}
INPUT
12345
EXPECTED

# I'm finished
done_testing();

0 comments on commit 8ff3d33

Please sign in to comment.