« blib/lib/WWW/Shopify/Liquid/Operator/Minus.pm
↑ Summary
blib/lib/WWW/Shopify/Liquid/Tag/Include.pm »
| 1 | #!/usr/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | package WWW::Shopify::Liquid::Tag::If; | ||
| 6 | use base 'WWW::Shopify::Liquid::Tag::Enclosing'; | ||
| 7 | use Scalar::Util qw(blessed); | ||
| 8 | |||
| 9 | sub min_arguments { return 1; } | ||
| 10 | sub max_arguments { return 1; } | ||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $package = shift; | ||
| 14 | my $self = bless { | ||
| 15 | line => shift, | ||
| 16 | core => shift, | ||
| 17 | arguments => shift, | ||
| 18 | true_path => undef, | ||
| 19 | false_path => undef | ||
| 20 | }, $package; | ||
| 21 | $self->interpret_inner_tokens(@{$_[0]}); | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | sub inner_tags { return qw(elsif else) } | ||
| 25 | sub subelements { qw(true_path false_path arguments); } | ||
| 26 | |||
| 27 | sub interpret_inner_tokens { | ||
| 28 | my ($self, @tokens) = @_; | ||
| 29 | # Comes in [true_path], [tag, other_path], [tag, other_path], ... | ||
| 30 | my $token = shift(@tokens); | ||
| 31 | return undef unless $token; | ||
| 32 | $self->{true_path} = $token->[0]; | ||
| 33 | if (int(@tokens) > 0) { | ||
| 34 | die new WWW::Shopify::Liquid::Exception::Parser($self, "else cannot be anywhere, except the end tag of an if statement.") if $tokens[0]->[0]->tag eq "else" && int(@tokens) > 1; | ||
| 35 | if ($tokens[0]->[0]->tag eq "elsif") { | ||
| 36 | $token = shift(@tokens); | ||
| 37 | my $arguments = $token->[0]->{arguments}; | ||
| 38 | $self->{false_path} = WWW::Shopify::Liquid::Tag::If->new($token->[0]->{line}, "if", $arguments, [[$token->[1]], @tokens]); | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $self->{false_path} = $tokens[0]->[1]; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | sub render { | ||
| 47 | my ($self, $renderer, $hash) = @_; | ||
| 48 | return $renderer->state->value($self) if $renderer->state && $renderer->state->value($self); | ||
| 49 | my $arguments = $self->render_subelement($renderer, $hash, $self->{arguments}); | ||
| 50 | my $result; | ||
| 51 | eval { | ||
| 52 | my $path = $self->{((!$self->inversion && $arguments->[0]) || ($self->inversion && !$arguments->[0])) ? 'true_path' : 'false_path'}; | ||
| 53 | $result = $path && !$self->is_processed($path) ? $path->render($renderer, $hash) : $path; | ||
| 54 | }; | ||
| 55 | if (my $exp = $@) { | ||
| 56 | $exp->value($self->{arguments}, $arguments) if blessed($exp) && $exp->isa('WWW::Shopify::Liquid::Exception::Control::Pause'); | ||
| 57 | die $exp; | ||
| 58 | } | ||
| 59 | return defined $result ? $result : ''; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub inversion { return 0; } | ||
| 63 | |||
| 64 | sub optimize { | ||
| 65 | my ($self, $optimizer, $hash) = @_; | ||
| 66 | $self->{arguments}->[0] = $self->{arguments}->[0]->optimize($optimizer, $hash) if !$self->is_processed($self->{arguments}->[0]); | ||
| 67 | if ($self->is_processed($self->{arguments}->[0])) { | ||
| 68 | my $path = $self->{((!$self->inversion && $self->{arguments}->[0]) || ($self->inversion && !$self->{arguments}->[0])) ? 'true_path' : 'false_path'}; | ||
| 69 | return $self->is_processed($path) ? $path : $path->optimize($optimizer, $hash); | ||
| 70 | } | ||
| 71 | if (!$self->is_processed($self->{false_path})) { | ||
| 72 | $optimizer->push_conditional_state; | ||
| 73 | $self->{false_path} = $self->{false_path}->optimize($optimizer, $hash); | ||
| 74 | $optimizer->pop_conditional_state($hash); | ||
| 75 | } | ||
| 76 | if (!$self->is_processed($self->{true_path})) { | ||
| 77 | $optimizer->push_conditional_state; | ||
| 78 | $self->{true_path} = $self->{true_path}->optimize($optimizer, $hash); | ||
| 79 | $optimizer->pop_conditional_state($hash); | ||
| 80 | } | ||
| 81 | return $self; | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |