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

Allows conditional parsing based upon the setting of various Interchange session and database values:

config

The Interchange configuration variables. These are set by the directives in your Interchange configuration file (or the defaults).

cgi

Test CGI variable, posted into the page with a HTML form, or via a URI argument.

data

The Interchange database tables. Retrieves a column in the named table and returns true or false, based upon the value.

discount

Checks to see if a discount is present for an item.

errors

Check to see whether there are any error/information messages associated with a named form value.

[if errors fname]
	Please enter your first name.
[/if]

explicit

A test for an explicit value. If Perl code is placed in a [condition] container then the supplied code will be used to make the comparison.

field

This is much like the data test type, listed above, except that it works on the the table(s) listed in the DefaultTables local configuration directive.

file

Tests for the existence of a file. Useful for placing image tags only if the image is present.

file-A

Compares against a file's access time (in days).

file-B

Compares against a file's binary status

file-d

Tests whether a file is a directory.

file-e

Tests whether the file (or directory) exists at all.

file-f

Tests whether a file is a plain file (follows symbolic links).

file-l

Tests whether a file is a symbolic link.

file-M

Tests against the number of days since the file was modified.

file-r

Tests whether file is readable by the Interchange user.

file-s

Allows tests against the size of a file.

file-T

Tests whether a file is a plain text file.

file-w

Tests whether the file can be written to by the Interchange user.

file-x

Tests whether the file is executable by the Interchange user.

global

Configuration variables set using a global Variable directive.

items

Usually used as a litmus test to see if anything is in the cart, for example: If no cart name is specified then "main" will be used.

ordered

Order status of individual items in the Interchange shopping carts. If no cart name is specified then "main" will be used.

pragma

Test a page Pragma value, set with the the Pragma directive in the catalog.cfg file, or with the pragma tag.

scratch

Test a scratchpad variables, previously set with [set], [seti], [tmp] and [tmpn] (or not set, as the case may be).

scratchd

This is the same as the "scratch" test type, except that the variable is deleted from the scratchpad after testing.

[Note] Note

Introduced in version 5.5.1.

session

Test an Interchange session variable.

tmp

Test for existence of non-session temporary value, set with either the [ts] or [tn] tags, or via $Tmp in Perl.

[Note] Note

Introduced in version 5.8.2.

validcc

A special case which takes the form [if validcc no type exp_date]. Evaluates to true if the supplied credit card number, type of card and expiration date pass a validity test. Does a LUHN-10 calculation to weed out typos or phony card numbers. Uses the standard CreditCardAuto values for targets if nothing else is specified.

value

Test a form value, previously set with [value] or via a previous HTML form post.

variable

Configuration variables set using a local Variable or VariableDatabase directive.

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.9.0 (git-head)

SOURCE

Interchange 5.9.0:

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

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!