Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for {% continue %} interrupt in for loops
- Loading branch information
Showing
6 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |