For more information... RTFM!
NAVIGATION
PAGES THAT LINK HERE
ACCOUNT LOGIN

You are not logged in

Powered by Interchange version 5.7.0

if

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

Summary

  • [if type term op compare] ... [/if]

Parameter Description Default
type The type of test you would like to perform. None
base Alias for type. None
term Reference to a value, within the type context. None
op The comparison operator to be used. None
operator Alias for op. None
compare Value to be compared with the term. None
comp Alias for compare. None
condition Alias for compare. None
hide Suppress any output text that would ordinarily be returned from this tag.  (This universal parameter was introduced with Interchange version 5.5.2.) No
interpolate Parse Interchange tags, included in the body text, before this container tag executes. No
reparse Process any Interchange tags found in the text output from this tag. Yes

Examples

Tag expansion example (positional parameters)

[tmpn foo]bar[/tmpn]

[if scratch foo eq "bar"]
    Correct - scratch foo is barI'd award you a prize if I had one.
[else]
    BzzztWRONGThe correct answer is [scratch foo].
    You leave with nothing.
[/else]
[/if]
    Correct - scratch foo is barI'd award you a prize if I had one.

Tag expansion example (named parameters)

[tmpn foo]bar[/tmpn]

[if type="scratch" term="foo" op="eq" compare="bar"]
    Correct - scratch foo is barWell played!
[else]
    BzzztWRONGThe correct answer is [scratch foo].
    You are the weakest link..Goodbye.
[/else]
[/if]
    Correct - scratch foo is barWell played!

Description

This tag allows the conditional building of HTML and/or Interchange tag regions, based upon the setting of various Interchange session and database values.  The general form is as follows:

[if type term op compare]
[then]
    If true then this is shown as part of the document output.
    The "then" container is optional in most casesIf ! is prepended
    to the type setting, the sense is reversed and this will be output
    for a false condition.
[/then]
[elsif type term op compare]
    Optional, tested if the initial "if" test fails
[/elsif]
[else]
    Optional output when all other tests fail
[/else]
[/if]

The [if] tag can also have some variants:

[if type=explicit compare=`$perl_code`]
    Output if valid Perl code block returns a true value when executed.
[/if]

You can also use the Perl regular expression match syntax:

[if value fname =~ /^mike/i]
    This is the if with Mike.
[elsif value fname =~ /^kevin/i]
    This is an elsif with Kevin.
[/elsif]
[elsif value fname =~ /^racke/i]
    This is an elsif with Racke.
[/elsif]
[else]
    Imposter alert!
[/else]
[/if]

While the named parameter tag syntax works for [if], it is more convenient to use positional calls in most cases.  The only exception is if you are planning on doing a test on the results of another tag sequence:

[if value lname =~ /[value b_lname]/]
    Shipping name matches billing name.
[/if]

Oops!  That will not work.  You must do this instead:

[if type=value term=lname op="=~" compare="/[value b_lname]/"]
    Shipping name matches billing name.
[/if]

or better still:

[if type=explicit compare=`$Values->{lname} =~ /$Values->{b_lname}/`]
    Shipping name matches billing name.
[/if]

Interchange also supports a limited [and] and [or] capability:

[if value fname =~ /Mike/]
[or value fname =~ /Kevin/]
    Your name is Mike or Kevin.
[/if]

[if value fname =~ /Mike/]
[and value state =~ /OH/]
    Your name is Mike and you live in Ohio.
[/if]

If you need to perform very complex [and] and [or] operations then you will be better off using [if explicit] or, better still, using embedded Perl.  Either of these would allow more complex testing and parsing of values.

Test types

The test type is specified using the type parameter.  Any of the types, listed in this section, can be used.

config

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

[if config CreditCardAuto]
    Auto credit card validation is enabled.
[/if]

cgi

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

For example:

[if cgi foo = 1]
    You requested foo.
[else]
    Please ask for a foo next time.
[/else]
[/if]

data

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

[if data products::size::99-102]
    There is size information.
[else]
    No size information.
[/else]
[/if]

[if data products::size::99-102 =~ /small/i]
    There is a small size available.
    You look more like a XXXXXXL to me.
[else]
    99-102 is not available in small.
    Never mind, it wouldn't have fit you anyway.
[/else]
[/if]

discount

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

[if discount 99-102]
    Item is discounted.
[/if]

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.  For example:

[if explicit]
[condition]
    return 1 if $Values->{country} =~ /u\.?s\.?a?/i;
    return 0;
[/condition]
    You have indicated a US address.
[else]
    You have indicated a non-US address.
[/else]
[/if]

The previous example was a bit contrived, as the same effect could be accomplished with the following:

[if value country =~ /u\.?s\.?a?/i]
    ...
[/if]

but you will run into many situations where it is useful.

This will work for Variable values:

[if type=explicit compare="__MYVAR__"]
    ...
[/if]

Note that the compare parameter is equivalent to the [condition] container, in that both evaluate as Perl code.  That means you need to watch out when you put in user-supplied values, (so that users can't inject Perl code into your server) and data from your own variables or tables which may look different than you expected.

For example, say you're in a loop, checking whether at least one of the fields "foo" and "bar" has a true value:

[if type=explicit compare="[loop-param foo][loop-param bar]"]
    ...
[/if]

Most of the time this will work fine.  However, if "foo" contains a string beginning with '0', such as '0009', Perl will try to interpret it as an octal number, where the digit '9' is invalid, resulting in the following unexpected error in the website's error.log file:

Bad if 'explicit   0009': Illegal octal digit '9' at (eval 155) line 1, at end of line

A safer way to check is:

[if type=explicit compare="q{[loop-param foo][loop-param bar]}"]
    ...
[/if]

Although, in that case, your data must not contain a "}".  To be extra safe you can surround your interpolated data with a [filter] that's appropriate for the quoting method you've chosen.

This code can be useful on a form submission page, especially if the user is sent back to the form to correct errors:

[if type=explicit compare="[error all=1 show_var=1 keep=1]"]
    <b>The following errors were detected in your form submission:</b>
    <br>
    <blockquote>
        [error all=1 keep=1 show_error=1 show_label=1 joiner="<br>"]
    </blockquote>
[/if]

See the [error] tag for more information.  Also see the errors test, above.

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.

[if field size::99-102]
    There is size information.
[else]
    No size information.
[/else]
[/if]

[if field size::99-102 =~ /small/i]
    There is a small size available.
    You look more like a XXXXXXL to me.
[else]
    99-102 is not available in small.
    Never mind, it wouldn't have fit you anyway.
[/else]
[/if]

file

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

[if type=file term="path/to/images/[item-code].gif"]
    <img src="/path/to/images/[item-code].gif">
[/if]

file-A

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

[if file-A foofile.ext > 1]
    foofile.ext has not been accessed in over a day.
[/if]

This is the same as Perl's -A test.

file-B

Compares against a file's binary status.

[if file-B foofile.ext]
    foofile.ext is a binary file.
[/if]

This is the same as Perl's -B test.  Note that the test only examines the first 1000 bytes or so, and so it can be fooled.

file-d

Tests whether a file is a directory.

[if file-d foodir/bardir]
    foodir/bardir is a directory.
[/if]

This is the same as Perl's -d test.

file-e

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

[if file-e foo/bar]
    foo/bar exists
[/if]

This is the same as Perl's -e test.

file-f

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

[if file-f foo/bar]
    foo/bar is a file.
[/if]

This is the same as Perl's -f test, and is the default for [if file].  The following two examples are equivalent:

[if file-f error.log]
    error.log is a file.
[/if]

[if file error.log]
    error.log is a file.
[/if]

file-l

Tests whether a file is a symbolic link.

[if file-l foo/bar]
    foo/bar is a symbolic link.
[/if]

This is the same as Perl's -l test.

file-M

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

[if file-M error.log > 0.5]
    There have been no errors in 12 hours.
[/if]

[if file-M error.log < 1]
    There have been errors in the last day.
[/if]

This is the same as Perl's -M test.

file-r

Tests whether file is readable by the Interchange user.

[if file-r error.log]
    We can read the error.log.
[/if]

This is the same as Perl's -r test.

file-s

Allows tests against the size of a file.

[if file-s error.log > 1024]
    There have been more than a kilobyte of errors.
[/if]

[if file-s error.log < 100000]
    There have been than less than 100,000 bytes of errors.
[/if]

This is the same as Perl's -s test.

file-T

Tests whether a file is a plain text file.

[if file-T error.log]
    error.log is a text file.
[/if]

This is the same as Perl's -T test.  Note that the test only examines the first 1000 bytes or so, and so it can be fooled.

file-w

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

[if file-w error.log]
    We can log errors.
[else]
    We can't log errorsCall the Coast Guard!
[/else]
[/if]

This is the same as Perl's -w test.

file-x

Tests whether the file is executable by the Interchange user.

[if file-x error.log]
    error.log is not executable (not that it needs to be).
[/if]

[if !file-x pages]
    There is no way to use a page in the pages directory, as it is
    not searchable.
[/if]

This is the same as Perl's -x test.

global

Configuration variables set using a global Variable directive.

[if global MV_PAGE eq 'index']
    You are on the index page.
[else]
    You are not in the index page.
    You are on @@MV_PAGE@@.
[/else]
[/if]

If you want to test a local Variable, or a variable set using the local VariableDatabase configuration directive, then use this tag's variable parameter.

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.

[if items]
    You have items in your shopping cart.
[/if]

[if items layaway]
    You have items on layaway.
[/if]

ordered

Order status of individual items in the Interchange shopping carts.  If no cart name is specified then "main" will be used.  The following items refer to a part number of 99-102.

Check the status of an item on order (using the default "main" cart).

[if ordered 99-102]
    Item 99-102 is in your cart.
[/if]

Check the status of an item on order (using the named "layaway" cart).

[if ordered 99-102 layaway]
    Item 99-102 is on layaway.
[/if]

Check the status of an item on order in the main cart.  Specifically, we are checking whether the ordered item has a "size" attribute.

[if ordered 99-102 main size]
    A size was specified for 99-102
[/if]

Check the status of an item on order in the main cart.  Specifically, we are checking whether the ordered item's "size" attribute value contains the word "large".

[if ordered 99-102 main size =~ /large/i]
    The size attribute for 99-102 contains "large"
[/if]

To make sure it is exactly "large", you could use:

[if ordered 99-102 main size eq "large"]
    The size attribute for 99-102 is "large"
[/if]

pragma

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

[if pragma dynamic_variables]
    __THE_VARIABLE__
[else]
    [data table=variable column=Variable key=THE_VARIABLE]
[/else]
[/if]

scratch

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

[if scratch mv_separate_items]
    ordered items will be placed on a separate line.
[else]
    ordered items will be placed on the same line.
[/else]
[/if]

scratchd

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

[if scratchd foo == 1]
    Foo was equal to 1, but has now been deleted.
[else]
    Foo was not equal to 1.
    It has been deleted now so you'll never know what the value actually was.
[/else]
[/if]
Availability

Availability

This test type was introduced in version 5.5.1, and is therefore not available for use with any earlier Interchange version.

session

Test an Interchange session variable.  Of particular interest are "logged_in", "username", "spider" and "browser".  Other session hash keys include the following:

Name Description
accesses The number of pare requests performed for this session within the last 30 seconds.
arg The argument passed in an [area] tag.
browser The identification value sent by the user agent.
carts A reference to a Perl hash containing shopping carts.  See $Carts for more information.
host Interchange's note of the visitor's hostname (possibly modified by DomainTail).  If the hostname could not be looked up (HostnameLookups switched off or DNS error etc.) then the visitor's IP address will be stored here instead.
id The current session ID.
last_error The last error or status message from the error logging routine.
last_search The last non-SQL Interchange search specification invoked.
last_url The current Interchange path_info.
logged_in Will be true if the user is logged in.
ohost Interchange's note of the visitor's IP address when using a non-SSL link.  Also see shost.
pageCount Number of unique URIs generated by Interchange for this session.
payment_result A hash of result messages from payment module transaction attempts.  See examples in the Interchange payment modules documentation.
prev_url The previous path_info.
referer HTTP_REFERER string.
ship_message The last error messages from shipping.
shost Interchange's note of the visitor's IP address when using a SSL link.  Also see ohost.
spider Will be true if the user is a search engine spider.
source Source of original entry to Interchange.
teleport Date and time (in "%Y%m%d%H%M%S" format) for use with the PageTables directive.
time Time (seconds since 01 January 1970) of the last access.
user The REMOTE_USER string.
username Logged-in username.  See the Interchange user database page.

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.

An example might be the when search matches are displayed.  If you use the string "[value mv_match_count] titles found", the following will display a different message if only one match was found:

[if value mv_match_count != 1]
    [value mv_match_count] matches found.
[else]
    Only one match was found.
[/else]
[/if]

variable

Configuration variables set using a local Variable or VariableDatabase directive.

[if variable FOO_ALLOWED]
    You may foo if you so wish.
[else]
    No foos here please.
    we are fed up with people fooing around.
[/else]
[/if]

If you want to test a global Variable then use this tag's global parameter.

Sub-tags

[then]

This is optional if you are not nesting if conditions, as the text immediately following the [if] tag is used as the conditionally substituted text.

For a usage example, see the description, above.

Note

Note

It used to be the case that, when nesting [if] tags, you needed to use a [then][/then] container on any outside conditions to ensure proper interpolation.  This is no longer the case with modern Interchange versions.

[elsif type term op compare]

This is an additional condition to test, which is applied if the initial [if] test fails.

For a usage example, see the description, above.

[else]

This block is output if all other test fail.

For a usage example, see the description, above.

[condition]

This is only used with the [if explicit] test type.  It allows an arbitrary Perl expressions be placed inside, with its return value interpreted as the result of the test.  See the explicit test type for a usage example.

[and]

This sub-tag can be used to add a simple "and" condition to the [if].  For example:

[if value fname =~ /Mike/]
[and value state =~ /OH/]
    Your name is Mike and you live in Ohio.
    Don't ask how I knew that.
[/if]

[or]

This sub-tag can be used to add a simple "or" condition to the [if].  For example:

[if value fname =~ /Mike/]
[or value fname =~ /Kevin/]
    Your name is either Mike or Kevin.
[/if]

Parameters

type

This parameter is used to specify the type of test you would like to perform.  Many test types are available.

term

The value of this parameter is treated differently, depending upon the context provided by the type parameter.

The value referenced by this parameter will be compared with the compare value.

op

This parameter specifies the compare operator which is to be used.  Compare operators are as in Perl:

Symbol Description
== Numeric equivalence.
eq String equivalence.
> Numeric greater-than.
>= Numeric greater-than or equal to.
gt String greater-than.
< Numeric less-than.
<= Numeric less-than or equal to.
lt String less-than.
!= Numeric non-equivalence.
ne String non-equivalence.
=~ Regular expression match.
!~ Regular expression non-match.

Any simple Perl test can be used, including some limited regex matching.  More complex tests are best done with [if explicit].

compare

This value will be compared with the value referenced by the term parameter.

Category:  Interchange tags
Last modified by: Peter Ajamian
Modification date: Tuesday 25 November 2008 at 12:13 AM (CST)
Home  |  Legal nonsense  |  Privacy policy  |  Donations  |  Contact us