| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Template::Liquid::Tag::Case; | 
| 2 |  |  |  |  |  |  | our $VERSION = '1.0.23'; | 
| 3 | 25 |  |  | 25 |  | 175 | use strict; | 
|  | 25 |  |  |  |  | 61 |  | 
|  | 25 |  |  |  |  | 739 |  | 
| 4 | 25 |  |  | 25 |  | 164 | use warnings; | 
|  | 25 |  |  |  |  | 50 |  | 
|  | 25 |  |  |  |  | 644 |  | 
| 5 | 25 |  |  | 25 |  | 137 | use base 'Template::Liquid::Tag::If'; | 
|  | 25 |  |  |  |  | 52 |  | 
|  | 25 |  |  |  |  | 12403 |  | 
| 6 |  |  |  |  |  |  | require Template::Liquid::Error; | 
| 7 |  |  |  |  |  |  | require Template::Liquid::Utility; | 
| 8 | 25 |  |  | 25 |  | 101 | sub import { Template::Liquid::register_tag('case') } | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | sub new { | 
| 11 | 11 |  |  | 11 | 0 | 25 | my ($class, $args) = @_; | 
| 12 |  |  |  |  |  |  | raise Template::Liquid::Error {type     => 'Context', | 
| 13 |  |  |  |  |  |  | template => $args->{template}, | 
| 14 |  |  |  |  |  |  | message  => 'Missing template argument', | 
| 15 |  |  |  |  |  |  | fatal    => 1 | 
| 16 |  |  |  |  |  |  | } | 
| 17 | 11 | 50 |  |  |  | 31 | if !defined $args->{'template'}; | 
| 18 |  |  |  |  |  |  | raise Template::Liquid::Error {type     => 'Context', | 
| 19 |  |  |  |  |  |  | template => $args->{template}, | 
| 20 |  |  |  |  |  |  | message  => 'Missing parent argument', | 
| 21 |  |  |  |  |  |  | fatal    => 1 | 
| 22 |  |  |  |  |  |  | } | 
| 23 | 11 | 50 |  |  |  | 29 | if !defined $args->{'parent'}; | 
| 24 |  |  |  |  |  |  | raise Template::Liquid::Error { | 
| 25 |  |  |  |  |  |  | type     => 'Syntax', | 
| 26 |  |  |  |  |  |  | template => $args->{template}, | 
| 27 |  |  |  |  |  |  | message => 'Missing argument list in ' . $args->{'markup'}, | 
| 28 |  |  |  |  |  |  | fatal   => 1 | 
| 29 |  |  |  |  |  |  | } | 
| 30 | 11 | 50 |  |  |  | 29 | if !defined $args->{'attrs'}; | 
| 31 | 11 | 50 |  |  |  | 50 | if ($args->{'attrs'} !~ m[\S$]o) { | 
| 32 |  |  |  |  |  |  | raise Template::Liquid::Error { | 
| 33 |  |  |  |  |  |  | type     => 'Syntax', | 
| 34 |  |  |  |  |  |  | template => $args->{template}, | 
| 35 | 0 |  |  |  |  | 0 | message => 'Bad argument list in ' . $args->{'markup'}, | 
| 36 |  |  |  |  |  |  | fatal   => 1 | 
| 37 |  |  |  |  |  |  | }; | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  | my $s = bless {name     => $args->{'tag_name'} . '-' . $args->{'attrs'}, | 
| 40 |  |  |  |  |  |  | blocks   => [], | 
| 41 |  |  |  |  |  |  | tag_name => $args->{'tag_name'}, | 
| 42 |  |  |  |  |  |  | template => $args->{'template'}, | 
| 43 |  |  |  |  |  |  | parent   => $args->{'parent'}, | 
| 44 |  |  |  |  |  |  | markup   => $args->{'markup'}, | 
| 45 |  |  |  |  |  |  | value    => $args->{'attrs'}, | 
| 46 |  |  |  |  |  |  | first_block     => 0, | 
| 47 | 11 |  |  |  |  | 378 | end_tag         => 'end' . $args->{'tag_name'}, | 
| 48 |  |  |  |  |  |  | conditional_tag => qr[^(?:else|when)$]o | 
| 49 |  |  |  |  |  |  | }, $class; | 
| 50 | 11 |  |  |  |  | 47 | return $s; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  | sub push_block { | 
| 54 | 26 |  |  | 26 | 0 | 65 | my ($s, $args) = @_; | 
| 55 |  |  |  |  |  |  | raise Template::Liquid::Error {type     => 'Context', | 
| 56 |  |  |  |  |  |  | template => $s->{template}, | 
| 57 |  |  |  |  |  |  | message  => 'Missing template argument', | 
| 58 |  |  |  |  |  |  | fatal    => 1 | 
| 59 |  |  |  |  |  |  | } | 
| 60 | 26 | 50 |  |  |  | 60 | if !defined $args->{'template'}; | 
| 61 |  |  |  |  |  |  | raise Template::Liquid::Error {type     => 'Context', | 
| 62 |  |  |  |  |  |  | template => $s->{template}, | 
| 63 |  |  |  |  |  |  | message  => 'Missing parent argument', | 
| 64 |  |  |  |  |  |  | fatal    => 1 | 
| 65 |  |  |  |  |  |  | } | 
| 66 | 26 | 50 |  |  |  | 57 | if !defined $args->{'parent'}; | 
| 67 |  |  |  |  |  |  | raise Template::Liquid::Error { | 
| 68 |  |  |  |  |  |  | type     => 'Syntax', | 
| 69 |  |  |  |  |  |  | template => $s->{template}, | 
| 70 |  |  |  |  |  |  | message => 'Missing argument list in ' . $args->{'markup'}, | 
| 71 |  |  |  |  |  |  | fatal   => 1 | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 26 | 50 | 66 |  |  | 84 | if !defined $args->{'attrs'} && $args->{'tag_name'} eq 'when'; | 
| 74 | 26 | 100 |  |  |  | 62 | if ($args->{'tag_name'} eq 'when') { | 
| 75 |  |  |  |  |  |  | $args->{'attrs'} = join ' or ', | 
| 76 | 32 |  |  |  |  | 162 | map  { sprintf '%s == %s', $args->{'parent'}{'value'}, $_ } | 
| 77 | 32 |  |  |  |  | 98 | grep { defined $_ } | 
| 78 | 23 |  |  |  |  | 677 | $args->{'attrs'} =~ m[(${Template::Liquid::Utility::Expression}) | 
| 79 |  |  |  |  |  |  | (?:\s+or\s+|\s*\,\s*)?]oxmg; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  | my $block | 
| 82 |  |  |  |  |  |  | = Template::Liquid::Block->new( | 
| 83 |  |  |  |  |  |  | {tag_name => $args->{'tag_name'}, | 
| 84 |  |  |  |  |  |  | end_tag => 'end' . $args->{'tag_name'}, | 
| 85 |  |  |  |  |  |  | attrs   => $args->{'attrs'}, | 
| 86 | 26 |  |  |  |  | 165 | template => $args->{'template'}, | 
| 87 |  |  |  |  |  |  | parent   => $s | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  | ); | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | # finish previous block if it exists | 
| 92 | 26 |  |  |  |  | 61 | ${$s->{'blocks'}[-1]}{'nodelist'} = $s->{'nodelist'} | 
| 93 | 26 | 50 |  |  |  | 57 | if scalar @{$s->{'blocks'}}; | 
|  | 26 |  |  |  |  | 74 |  | 
| 94 | 26 |  |  |  |  | 54 | $s->{'nodelist'} = [];    # Unline {%if%}, we *always* empty the | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | # nodelist. This way, we ignore nodes that come before the first | 
| 97 |  |  |  |  |  |  | # when/else block just like Liquid | 
| 98 | 26 |  |  |  |  | 38 | push @{$s->{'blocks'}}, $block; | 
|  | 26 |  |  |  |  | 48 |  | 
| 99 | 11 |  |  |  |  | 20 | shift @{$s->{'blocks'}}    # S::D->parse() pushes a dead first block | 
| 100 | 26 | 100 |  |  |  | 69 | if $s->{'first_block'}++ == 0; | 
| 101 | 26 |  |  |  |  | 65 | return $block; | 
| 102 |  |  |  |  |  |  | } | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =pod | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =encoding UTF-8 | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =begin stopwords | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | Lütke jadedPixel | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =end stopwords | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 NAME | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | Template::Liquid::Tag::Case - Switch Statement Construct | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 Description | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | If you need more conditions, you can use the C<case> tag. Note that, stuff that | 
| 122 |  |  |  |  |  |  | comes before the first C<when> or C<else> is ignored. ...just as it is in | 
| 123 |  |  |  |  |  |  | Liquid. | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | =head1 Synopsis | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | {% case condition %} | 
| 128 |  |  |  |  |  |  | {% when 1 %} | 
| 129 |  |  |  |  |  |  | hit 1 | 
| 130 |  |  |  |  |  |  | {% when 2 or 3 %} | 
| 131 |  |  |  |  |  |  | hit 2 or 3 | 
| 132 |  |  |  |  |  |  | {% else %} | 
| 133 |  |  |  |  |  |  | ... else ... | 
| 134 |  |  |  |  |  |  | {% endcase %} | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | ...or even... | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | {% case template %} | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | {% when 'label' %} | 
| 141 |  |  |  |  |  |  | // {{ label.title }} | 
| 142 |  |  |  |  |  |  | {% when 'product' %} | 
| 143 |  |  |  |  |  |  | // {{ product.vendor | link_to_vendor }} / {{ product.title }} | 
| 144 |  |  |  |  |  |  | {% else %} | 
| 145 |  |  |  |  |  |  | // {{page_title} | 
| 146 |  |  |  |  |  |  | {% endcase %} | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 Author | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/ | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | The original Liquid template system was developed by jadedPixel | 
| 153 |  |  |  |  |  |  | (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/). | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 License and Legal | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt> | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it under | 
| 160 |  |  |  |  |  |  | the terms of The Artistic License 2.0.  See the F<LICENSE> file included with | 
| 161 |  |  |  |  |  |  | this distribution or http://www.perlfoundation.org/artistic_license_2_0.  For | 
| 162 |  |  |  |  |  |  | clarification, see http://www.perlfoundation.org/artistic_2_0_notes. | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | When separated from the distribution, all original POD documentation is covered | 
| 165 |  |  |  |  |  |  | by the Creative Commons Attribution-Share Alike 3.0 License.  See | 
| 166 |  |  |  |  |  |  | http://creativecommons.org/licenses/by-sa/3.0/us/legalcode.  For clarification, | 
| 167 |  |  |  |  |  |  | see http://creativecommons.org/licenses/by-sa/3.0/us/. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | =cut |