| 1 | package Template::Liquid::Tag::If; | ||||||||||||||||||||||||||||
| 2 | 24 | our $VERSION = '1.0.22'; | |||||||||||||||||||||||||||
| 3 | 24 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 24 | require Template::Liquid::Error; | |||||||||||||||||||||||||||
| 6 | 24 | require Template::Liquid::Utility; | |||||||||||||||||||||||||||
| 7 | 24 | ▶ | use base 'Template::Liquid::Tag'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 24 | ▶ | sub import { Template::Liquid::register_tag('if') } | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | sub new { | ||||||||||||||||||||||||||||
| 11 | 89 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | raise Template::Liquid::Error {type => 'Context', | ||||||||||||||||||||||||||||
| 13 | template => $args->{template}, | ||||||||||||||||||||||||||||
| 14 | message => 'Missing template argument', | ||||||||||||||||||||||||||||
| 15 | fatal => 1 | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | 89 | ▶ | if !defined $args->{'template'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | raise Template::Liquid::Error {type => 'Context', | ||||||||||||||||||||||||||||
| 19 | template => $args->{template}, | ||||||||||||||||||||||||||||
| 20 | message => 'Missing parent argument', | ||||||||||||||||||||||||||||
| 21 | fatal => 1 | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 89 | ▶ | if !defined $args->{'parent'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 | 89 | ▶ | if !defined $args->{'attrs'} || $args->{'attrs'} !~ m[\S$]o; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $args->{"attrs"} and $args->{"attrs"} =~ /\S$/o
MC/DC0%0/2
defined $args->{"attrs"} and $args->{"attrs"} =~ /\S$/o
defined $args->{"attrs"}
$args->{"attrs"} =~ /\S$/o
Truth table33%1/3
A and B
A= defined $args->{"attrs"}B= $args->{"attrs"} =~ /\S$/o
| |||||||||||||||||||||||||||||
| 31 | 89 | my $condition = $args->{'attrs'}; | |||||||||||||||||||||||||||
| 32 | my $s = bless {name => $args->{'tag_name'} . '-' . $condition, | ||||||||||||||||||||||||||||
| 33 | blocks => [], | ||||||||||||||||||||||||||||
| 34 | tag_name => $args->{'tag_name'}, | ||||||||||||||||||||||||||||
| 35 | template => $args->{'template'}, | ||||||||||||||||||||||||||||
| 36 | parent => $args->{'parent'}, | ||||||||||||||||||||||||||||
| 37 | markup => $args->{'markup'}, | ||||||||||||||||||||||||||||
| 38 | 89 | end_tag => 'end' . $args->{'tag_name'}, | |||||||||||||||||||||||||||
| 39 | conditional_tag => qr[^(?:else|else?if)$]o | ||||||||||||||||||||||||||||
| 40 | }, $class; | ||||||||||||||||||||||||||||
| 41 | 89 | return $s; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub push_block { | ||||||||||||||||||||||||||||
| 45 | 35 | ▶ | my ($s, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
push_block: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 46 | my $block | ||||||||||||||||||||||||||||
| 47 | = Template::Liquid::Block->new({tag_name => $args->{'tag_name'}, | ||||||||||||||||||||||||||||
| 48 | attrs => $args->{'attrs'}, | ||||||||||||||||||||||||||||
| 49 | 35 | template => $args->{'template'}, | |||||||||||||||||||||||||||
| 50 | parent => $s | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | { # finish previous block | ||||||||||||||||||||||||||||
| 54 | 35 | ${$s->{'blocks'}[-1]}{'nodelist'} = $s->{'nodelist'}; | |||||||||||||||||||||||||||
| 55 | 35 | $s->{'nodelist'} = []; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | 35 | push @{$s->{'blocks'}}, $block; | |||||||||||||||||||||||||||
| 58 | 35 | return $block; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | sub render { | ||||||||||||||||||||||||||||
| 62 | 130 | ▶ | my ($s) = @_; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 130 | for my $block (@{$s->{'blocks'}}) { | |||||||||||||||||||||||||||
| 64 | 167 | ▶ | return $block->render() if grep {$_} @{$block->{'conditions'}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 24 | 1; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =pod | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =begin stopwords | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Lütke jadedPixel | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | =end stopwords | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head1 NAME | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Template::Liquid::Tag::If - Basic If/Elsif/Else Construct | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 Description | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | If I need to describe if/else to you... Oy. C<if> executes the statement once | ||||||||||||||||||||||||||||
| 86 | if and I<only> if the condition is true. If the condition is false, the first | ||||||||||||||||||||||||||||
| 87 | C<elseif> condition is evaluated. If that is also false it continues in the | ||||||||||||||||||||||||||||
| 88 | same pattern until we find a true condition or a fallback C<else> tag. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head2 Compound Inequalities | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Liquid supports compound inequalities. Try these... | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | {% if some.value == 3 and some.string contains 'find me' %} | ||||||||||||||||||||||||||||
| 95 | Wow! It's a match... | ||||||||||||||||||||||||||||
| 96 | {% elseif some.value == 4 or 3 < some.value %} | ||||||||||||||||||||||||||||
| 97 | Wow! It's a... different... match... | ||||||||||||||||||||||||||||
| 98 | {% endif %} | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =head1 Bugs | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | Liquid's (and by extension L<Template::Liquid|Template::Liquid>'s) treatment of | ||||||||||||||||||||||||||||
| 103 | compound inequalities is broken. For example... | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | {% if 'This and that' contains 'that' and 1 == 3 %} | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | ...would be parsed as if it were... | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | if ( "'This" && ( "that'" =~ m[and] ) ) { ... | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | ...but it I<should> look like... | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | if ( ( 'This and that' =~ m[that]) && ( 1 == 3 ) ) { ... | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | It's just... not pretty but I'll work on it. The actual problem is in | ||||||||||||||||||||||||||||
| 116 | L<Template::Liquid::Block|Template::Liquid::Block> if you feel like lending a | ||||||||||||||||||||||||||||
| 117 | hand. Wink, wink. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head1 See Also | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | See L<Template::Liquid::Condition|Template::Liquid::Condition> for a list of | ||||||||||||||||||||||||||||
| 122 | supported inequality types. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 Author | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Sanko Robinson <sanko@cpan.org> - http://sankorobinson.com/ | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | The original Liquid template system was developed by jadedPixel | ||||||||||||||||||||||||||||
| 129 | (http://jadedpixel.com/) and Tobias Lütke (http://blog.leetsoft.com/). | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 License and Legal | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Copyright (C) 2009-2022 by Sanko Robinson E<lt>sanko@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 136 | the terms of The Artistic License 2.0. See the F<LICENSE> file included with | ||||||||||||||||||||||||||||
| 137 | this distribution or http://www.perlfoundation.org/artistic_license_2_0. For | ||||||||||||||||||||||||||||
| 138 | clarification, see http://www.perlfoundation.org/artistic_2_0_notes. | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | When separated from the distribution, all original POD documentation is covered | ||||||||||||||||||||||||||||
| 141 | by the Creative Commons Attribution-Share Alike 3.0 License. See | ||||||||||||||||||||||||||||
| 142 | http://creativecommons.org/licenses/by-sa/3.0/us/legalcode. For clarification, | ||||||||||||||||||||||||||||
| 143 | see http://creativecommons.org/licenses/by-sa/3.0/us/. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =cut | ||||||||||||||||||||||||||||