3.5. if
[if type field op* compare*]
The Interchange Tags Reference has more information on the [if] tag.
-
named attributes: [if type="type" term="field" op="op" compare="compare"]
[if !type field op* compare*]
-
named attributes: [if type="!type" term="field" op="op" compare="compare"]
Allows the conditional building of HTML based on the setting of various Interchange session and database values. The general form is:
[if type term op compare] [then] If true, this text is printed on the document. The [then] [/then] is optional in most cases. If ! is prepended to the type setting, the sense is reversed and this text will be output for a false condition. [/then] [elsif type term op compare] Optional, tested when if fails. [/elsif] [else] Optional, printed on the document when all above fail. [/else] [/if]
The [if] tag can also have some variants:
[if explicit] [condition] CODE [/condition] Displayed if valid Perl CODE returns a true value. [/if]
Some Perl-style regular expressions can be written:
[if value name =~ /^mike/i] This is the if with Mike. [elsif value name =~ /^sally/i] This is an elsif with Sally. [/elsif] [elsif value name =~ /^barb/i] [or value name =~ /^mary/i] This is an elsif with Barb or Mary. [elsif value name =~ /^pat/i] [and value othername =~ /^mike/i] This is an elsif with Pat and Mike. [/elsif] [else] This is the else, no name I know. [/else] [/if]
While the named parameter tag syntax works for [if ...], it is more convenient to use the positional syntax in most cases. The only exception is when you are planning to do a test on the results of another tag sequence:
This will not work:
[if value name =~ /[value b_name]/] Shipping name matches billing name. [/if]
Do this instead:
[if type=value term=name op="=~" compare="/[value b_name]/"] Shipping name matches billing name. [/if]
As an alternative:
[if type=value term=high_water op="<" compare="[shipping noformat=1]"] The shipping cost is too high, charter a truck. [/if]
There are many test targets available. The following is a list of some of the available test targets.
config Directive
-
The Interchange configuration variables. These are set by the directives in the Interchange configuration file.
[if config CreditCardAuto] Auto credit card validation is enabled. [/if]
data database::field::key
-
The Interchange databases. Retrieves a field in the database and returns true or false based on 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. [else] No small size available. [/else] [/if]
-
If another tag is needed to select the key, and it is not a looping tag construct, named parameters must be used:
[set code]99-102[/set] [if type=data term="products::size::[scratch code]"] There is size information. [else] No size information. [/else] [/if]
discount
-
Checks to see if a discount is present for an item.
[if discount 99-102] This item is discounted. [/if]
explicit
-
A test for an explicit value. If Perl code is placed between a [condition] [/condition] tag pair, it will be used to make the comparison. Arguments can be passed to import data from user space, just as with the [perl] tag.
[if explicit] [condition] $country = $ values =~{country}; return 1 if $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 same thing could be accomplished with [if value country =~ /u\.?s\.?a?/i], but there are many situations where this example could be useful.
file
-
Tests for the existence of a file. This is useful for placing image tags only if the image is present.
[if file /home/user/www/images/[item-code].gif] <IMG SRC="[item-code].gif"> [/if] or [if type=file term="/home/user/www/images/[item-code].gif"] <IMG SRC="[item-code].gif"> [/if]
-
The file test requires that the SafeUntrap directive contain ftfile (which is the default).
items
-
The Interchange shopping carts. If not specified, the cart used is the main cart. This is usually used to test to see if anything is in the cart. For example:
[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. Unless otherwise specified, the cart used is the main cart. The following items refer to a part number of 99-102.
[if ordered 99-102] ... [/if] Checks the status of an item on order, true if item 99-102 is in the main cart. [if ordered 99-102 layaway] ... [/if] Checks the status of an item on order, true if item 99-102 is in the layaway cart. [if ordered 99-102 main size] ... [/if] Checks the status of an item on order in the main cart, true if it has a size attribute. [if ordered 99-102 main size =~ /large/i] ... [/if] Checks the status of an item on order in the main cart, true if it has a size attribute containing 'large'. THE CART NAME IS REQUIRED IN THE OLD SYNTAX. The new syntax for that one would be: [if type=ordered term="99-102" compare="size =~ /large/i"] To make sure it is the size that is large and not another attribute, you could use: [if ordered 99-102 main size eq 'large'] ... [/if] [if ordered 99-102 main lines] ... [/if] Special case -- counts the lines that the item code is present on. (Only useful, of course, when mv_separate_items or SeparateItems is defined.)
scratch
-
The Interchange scratchpad variables, which can be set with the [set name] value [/set] element.
[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]
session
-
The Interchange session variables. Of particular interest are logged_in, source, browser and username.
validcc
-
A special case, it 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. It performs a LUHN-10 calculation to weed out typos or phony card numbers.
value
-
The Interchange user variables, typically set in search, control or order forms. Variables beginning with mv_ are Interchange special values and should be tested and used with caution.
variable
-
See Interchange Variable values.
The field term is the specifier for that area. For example, [if session frames] would return true if the frames session parameter was set.
As an example, consider buttonbars for frame-based setups. You might decide to display a different buttonbar with no frame targets for sessions that are not using frames:
[if session frames] [buttonbar 1] [else] [buttonbar 2] [/else] [/if]
Another example might be the when search matches are displayed. If using the string [value mv_match_count] titles found, it will display a plural result even if there is only one match. Use:
[if value mv_match_count != 1] [value mv_match_count] matches found. [else] Only one match was found. [/else] [/if]
The op term is the compare operation to be used. Compare operations are the same as they are in Perl:
== numeric equivalence eq string equivalence > numeric greater-than gt string greater-than < numeric less-than lt string less-than != numeric non-equivalence ne string equivalence
Any simple Perl test can be used, including some limited regex matching. More complex tests should be done with [if explicit].
[then] text [/then]
-
This is optional if not nesting "if" conditions. The text immediately following the [if ..] tag is used as the conditionally substituted text. If nesting [if ...] tags, use [then][/then] on any outside conditions to ensure proper interpolation.
[elsif type field op* compare*]
-
named attributes: [elsif type="type" term="field" op="op" compare="compare"]
Additional conditions for test, applied if the initial [if ..] test fails.
[else] text [/else]
-
The optional else-text for an if or if-item-field conditional.
[condition] text [/condition]
-
Only used with the [if explicit] tag. Allows an arbitrary expression in Perl to be placed inside, with its return value interpreted as the result of the test. If arguments are added to [if explicit args], those will be passed as arguments in the [perl] construct.
[/if]
-
Terminates an if conditional.