A simple method of conditionally rendering content lies in LXP's native support for a small set of branching logic tags. These allow you to either display or hide regions of markup by performing equivalence checks on variables or cookies. LXP's basic branching logic tags include:
The <if> and <ifnot> tags operate on LXP variables (or object variable values), whereas the <ifcookie> and <ifnotcookie> tags operate on stored cookies for the current domain. In other words, the logical functions of <if> and <ifcookie> are the same; only the sources for logical evaluation differ.
The <else> tag is more generalized, and implements subsequent, inverted logic evaluations on any of the previously mentioned tags. The <elseif> and <elseifnot> tags are actually just shortcut tags with the same result as nesting an <if> or <ifnot> tag within an <else> region.
When used without any accompanying attributes, the <if> and <ifnot> tags perform no useful function. However, with meaningful attributes, these tags can be used to quickly and simply flag regions of mark-up for display under specific circumstances.
The <if> tag examines its defined attributes through equivalence comparisons to variables whose names match the attribute names. If the specified attribute's value matches the variable's value exactly, the region of mark-up between that <if> and its associated </if> closing tag will be processed by LXP. Otherwise, that region (between <if> and </if>) will be completely ignored (including any LXP mark-up) up to its closing tag.
You may include in the <if> tag either an attribute name, a complete attribute pair, or a series of attribute pairs, depending on the intended logical assessment you wish to make.
Providing only an attribute name (e.g., <if test>) causes LXP to check only for the existence of any characters assigned to the variable value with that name. In this case, if the variable is set to an empty value (or not set at all), the <if> match fails, and its defined region is muted (not displayed). Otherwise, if a value is found, the region is processed as it would be normally.
Providing one or more attribute pairs results in each attribute value being compared to the variable with the specified attribute name. When more than one attribute is specified in the tag, each condition must match exactly for the <if> conditions to be considered a match as a whole, and for the region to be processed.
Example 13-13 uses the <if> tag to check for the existence of any variable value named name, and compares the variable named access to the value of 1.
The <ifnot> tag logically performs the opposite of the <if> tag in every respect. For example, when multiple attributes are passed, each equivalence comparison must fail for the <ifnot> region to be processed.
Example 13-14 uses the <ifnot> tag to test for the lack of a variable called error, as well as to check that a variable named access is not set to the value of 0.
Example 13-14. Using the <ifnot> tag
<lxp> <ifnot error access="0"> <strong>Success!</strong><br /> An <em>error</em> is not set, and <em>access</em> is not set to 0.<br /> </ifnot> </lxp>
Note: You may not define two attributes with the same name in a single LXP tag (e.g., <ifnot access="0" access="2"> is not valid). Therefore, two logical assessments on one variable requires the use of two logic tags.
The term nesting refers to placing tags within regions marked-up by other tags. You may safely nest logical tags as much as you like, provided you carefully keep track of where they open and close.
In some cases, you may have to nest logic tags in order to perform multiple checks on a single variable. This is because you can only place a variable's name inside of a logic tag once.
Example 13-15 nests several logic tags within one top-level <if> tag.
Example 13-15. Using nested logic
<lxp> <if answer> <strong>You have supplied an answer!</strong><br /> <if answer="12"> Your answer is correct!<br /> </if> <ifnot answer="12"> Your answer of <putvar name="answer">, though, is incorrect.<br /> </ifnot> <if answer="12" cheatcode> You appear to be cheating, however. </if> </if> </lxp>
In Example 13-15, the first <if> tag checks to see if an argument titled answer is set at all. If it is not, the entire region it encapsulates is muted.
The second <if> tag evaluates the passed answer argument to see if it is equal to 12. If it is, that <if> tag's region is processed. Otherwise, that region will be muted.
The <ifnot> tag then checks to see if the passed argument named answer is not equal to 12. If it is not, the region that the <ifnot> encapsulates will be processed.
Lastly, the final <if> tag in Example 13-15 checks to see if the passed value for answer is equal to 12, and for the existence of a passed argument called cheatcode. If the variable answer is found to equal 12, and the variable cheatcode is found at all, the region encapsulated by the last <if> tag will be processed (meaning, in this case, that it is merely displayed).
The <ifcookie> and <ifnotcookie> tags behave identically to the <if> and <ifnot> tags, with the notable exception being that they derive the source of their logical evaluations from the cookies stored in the browser for the domain being accessed by the web browser, rather than from stored variables.
Example 13-16 welcomes a user with a personalized message if they have a cookie stored in their browser named username.
Example 13-16. Using ifcookie and ifnotcookie
<lxp> <ifcookie username> Welcome back, <putcookie name="username">.<br /> </ifcookie> <ifnotcookie username> <include src="login.php" /> </ifnotcookie> </lxp>
In Example 13-16, if the username cookie doesn't exist, the user will see a login screen provided by a PHP document. This document is rendered through an Apache sub-request inclusion (see the Section called Including External Content Types").
The <else>, <elseif>, and <elseifnot> tags aid in the creation of more involved conditional logic than a single <if> or <ifnot> statement.
The <else> tag marks a region to be displayed only if the last logical evaluation (at the same logical depth, if working with nested logic tags) was false. If the last logical evaluation was true, the <else> region will be muted.
Example 13-17 creates a simple <if> condition to check for the existence of a variable called answer. If it is not found, the region marked up by the <else> and </else> tags will be displayed; otherwise, that region will be muted.
Example 13-17. Using the <else> tag
<lxp> <if answer> Thank you for supplying an answer. </if> <else> You have not yet supplied an answer.<br /> <include src="forms/question.lxp" /> </else> </lxp>
As mentioned earlier in this section, the <elseif> and <elseifnot> tags are just shortcuts. They behave exactly as the <if> and <ifnot> tags do, respectively, if they were nested within an <else> region. For example, the following two blocks of markup are functionally identical:
<if condition1="true"> Condition 1 is True. </if> <else> <if condition2="true"> Condition 2 is true. </if> </else> ... <if condition1="true"> Condition 1 is True. </if> <elseif condition2="true"> Condition 2 is true. </elseif>
Using <else> tags streamlines both the maintainability and efficiency of the conditional logic. By using <else>, you can rely on LXP to keep track of whether or not the last condition was or was not met, and not have to re-evaluate the same conditions with the opposite logic tag.
Example 13-18. Using nested logic with <else> tags
<lxp> <if answer> <strong>You have supplied an answer!</strong><br /> <if answer="12"> Your answer is correct!<br /> <if cheatcode> You appear to be cheating, however. </if> <else> Congratulations for not cheating! </else> </if> <else> Your answer of <putvar name="answer">, though, is incorrect.<br /> </else> </if> <else> You have not yet supplied an answer.<br /> <include src="forms/question.lxp" /> </else> </lxp>