Name

if — conditional parsing

ATTRIBUTES

Attribute Pos. Req. Default Description
type Yes
term Yes
op Yes
compare Yes
interpolate     0 interpolate input?
reparse     1 interpolate output?
hide     0 Hide the tag return value?

DESCRIPTION

BEHAVIOR

This tag does not appear to be affected by, or affect, the rest of Interchange.

EXAMPLES

No examples are available at this time. We do consider this a problem and will try to supply some.

NOTES

AVAILABILITY

if is available in Interchange versions:

4.6.0-5.7.0 (git-head)

SOURCE

Interchange 5.7.0:

Source: lib/Vend/Interpolate.pm
Lines: 1461

sub tag_if {
my ($cond,$body,$negate) = @_;
#::logDebug("Called tag_if: $cond\n$body\n");
my ($base, $term, $op, $operator, $comp);
my ($else, $elsif, $else_present, @addl);

($base, $term, $operator, $comp) = split /\s+/, $cond, 4;
if ($base eq 'explicit') {
  $body =~ s#$QR{condition_begin}##o
    and ($comp = $1, $operator = '');
}
#::logDebug("tag_if: base=$base term=$term op=$operator comp=$comp");

#Handle unless
($base =~ s/^\W+// or $base = "!$base") if $negate;

$else_present = 1 if
  $body =~ /\[[EeTtAaOo][hHLlNnRr][SsEeDd\s]/;

($body, $elsif, $else, @addl) = split_if($body)
  if $else_present;

#::logDebug("Additional ops found:\n" . join("\n", @addl) ) if @addl;

unless(defined $operator) {
  undef $operator;
  undef $comp;
}

my $status = conditional ($base, $term, $operator, $comp, @addl);

#::logDebug("Result of if: $status\n");

my $out;
if($status) {
  $out = $body;
}
elsif ($elsif) {
  $else = '[else]' . $else . '[/else]' if length $else;
  my $pertinent = Vend::Parse::find_matching_end('elsif', \$elsif);
  unless(defined $pertinent) {
    $pertinent = $elsif;
    $elsif = '';
  }
  $elsif .= '[/elsif]' if $elsif =~ /\S/;
  $out = '[if ' . $pertinent . $elsif . $else . '[/if]';
}
elsif (length $else) {
  $out = $else;
}
return $out;
}

# This generates a *session-based* Autoload routine based
# on the contents of a preset Profile (see the Profile directive).
#
# Normally used for setting pricing profiles with CommonAdjust,
# ProductFiles, etc.
# 
sub restore_profile {
my $save;
return unless $save = $Vend::Session->{Profile_save};
for(keys %$save) {
  $Vend::Cfg->{$_} = $save->{$_};
}
return;
}

sub tag_profile {
my($profile, $opt) = @_;
#::logDebug("in tag_profile=$profile opt=" . uneval_it($opt));

$opt = {} if ! $opt;
my $tag = $opt->{tag} || 'default';

if(! $profile) {
  if($opt->{restore}) {
    restore_profile();
    if(ref $Vend::Session->{Autoload}) {
       @{$Vend::Session->{Autoload}} = 
         grep $_ !~ /^$tag-/, @{$Vend::Session->{Autoload}};
    }
  }
  return if ! ref $Vend::Session->{Autoload};
  $opt->{joiner} = ' ' unless defined $opt->{joiner};
  return join $opt->{joiner},
    grep /^\w+-\w+$/, @{ $Vend::Session->{Autoload} };
}

if($profile =~ s/(\w+)-//) {
  $opt->{tag} = $1;
  $opt->{run} = 1;
}
elsif (! $opt->{set} and ! $opt->{run}) {
  $opt->{set} = $opt->{run} = 1;
}

if( "$profile$tag" =~ /\W/ ) {
  logError(
    "profile: invalid characters (tag=%s profile=%s), must be [A-Za-z_]+",
    $tag,
    $profile,
  );
  return $opt->{failure};
}

if($opt->{run}) {
#::logDebug("running profile=$profile tag=$tag");
  my $prof = $Vend::Cfg->{Profile_repository}{$profile};
    if (not $prof) {
    logError( "profile %s (%s) non-existant.", $profile, $tag );
    return $opt->{failure};
  } 
#::logDebug("found profile=$profile");
  $Vend::Cfg->{Profile} = $prof;
  restore_profile();
#::logDebug("restored profile");
  PROFSET: 
  for my $one (keys %$prof) {
#::logDebug("doing profile $one");
    next unless defined $Vend::Cfg->{$one};
    my $string;
    my $val = $prof->{$one};
    if( ! ref $Vend::Cfg->{$one} ) {
      # Do nothing
    }
    elsif( ref($Vend::Cfg->{$one}) eq 'HASH') {
      if( ref($val) ne 'HASH') {
      $string = '{' .  $prof->{$one}  . '}'
        unless  $prof->{$one} =~ /^{/
        and    $prof->{$one} =~ /}\s*$/;
    }
    }
    elsif( ref($Vend::Cfg->{$one}) eq 'ARRAY') {
      if( ref($val) ne 'ARRAY') {
      $string = '[' .  $prof->{$one}  . ']'
        unless  $prof->{$one} =~ /^\[/
        and    $prof->{$one} =~ /]\s*$/;
    }
    }
    else {
      logError( "profile: cannot handle object of type %s.",
            $Vend::Cfg->{$one},
            );
      logError("profile: profile for $one not changed.");
      next;
    }

#::logDebug("profile value=$val, string=$string");
    undef $@;
    $val = $ready_safe->reval($string) if $string;

    if($@) {
      logError( "profile: bad object %s: %s", $one, $string );
      next;
    }
    $Vend::Session->{Profile_save}{$one} = $Vend::Cfg->{$one}
      unless defined $Vend::Session->{Profile_save}{$one};

#::logDebug("set $one to value=$val, string=$string");
    $Vend::Cfg->{$one} = $val;
  }
  return $opt->{success}
    unless $opt->{set};
}

#::logDebug("setting profile=$profile tag=$tag");
my $al;
if(! $Vend::Session->{Autoload}) {
  # Do nothing....
}
elsif(ref $Vend::Session->{Autoload}) {
  $al = $Vend::Session->{Autoload};
}
else {
  $al = [ $Vend::Session->{Autoload} ];
}

if($al) {
  @$al = grep $_ !~ m{^$tag-\w+$}, @$al;
}
$al = [] if ! $al;
push @$al, "$tag-$profile";
#::logDebug("profile=$profile Autoload=" . uneval_it($al));
$Vend::Session->{Autoload} = $al;

return $opt->{success};
}

AUTHORS

Interchange Development Group

SEE ALSO

DocBook! Interchange!