Appendix: Changing Content Visibility Using an Expression
As well as letting you display calculated values, expressions can be used to modify the appearance of site content. This includes conditionally hiding content based on whether an expression returns True or False, which is done using an ISTRUE element.
The syntax for this element is as follows, where the text inside the parentheses is the expression you want to evaluate:
<%= ISTRUE(Expression;) %>
Affected content goes here
<%= %>
Notice that unlike the other types of element introduced in this chapter, this syntax features a tag either side of the content you want to modify. Everything within this pair of tags is affected by the result of the opening tag's expression, and will be hidden unless the expression returns a value of True.
This is an example of a Wrap-around Element. Such elements are used to modify other content, and do not deliver any content of their own.
The following example renders the values of two keywords, and shows a message if the value of the first keyword is higher than that of the second:
<p>Your test score: <%# KEYWORD(VARScore) %></p>
<p>Score needed to pass: <%# KEYWORD(VARPass) %></p>
<%= ISTRUE(VARScore > VARPass;) %>
<p>You passed the test!</p>
<%= %>
A possible result of this markup, where the values satisfy the expression, is shown below:
Your test score: 75
Score needed to pass: 50
You passed the test!
On the other hand, if the values do not satisfy the expression, the result would look something like this:
Your test score: 45
Score needed to pass: 50
To provide an alternative message when the expression does not return True, you can use the NOTTRUE element as demonstrated below:
<p>Your test score: <%# KEYWORD(VARScore) %></p>
<p>Score needed to pass: <%# KEYWORD(VARPass) %></p>
<%= ISTRUE(VARScore > VARPass;) %>
<p>You passed the test!</p>
<%= %>
<%= ISNOTTRUE(VARScore > VARPass;) %>
<p>Sorry, you did not pass.</p>
<p>Better luck next time!</p>
<%= %>
This new element, as the name suggests, will only show the enclosed content if the result of its expression is not True. Now, when the first wrap-around element is not satisfied, the second ensures that a different message is rendered, consisting of two paragraphs instead of one:
Your test score: 45
Score needed to pass: 50
Sorry, you did not pass.
Better luck next time!
As with all expression-driven element tags, the contents of the parentheses can include multiple statements as long as they are separated using semicolons. In cases like this, the result of the expression will be whatever is returned by the final statement. In the new example below, the first statement assigns a value to the variable VARQuantity, while the second statement checks whether double that quantity is less than ten:
<%# ISTRUE(VARQuantity:= 5;VARQuantity + VARQuantity < 10;) %>
<p>Double the quantity is less than ten.</p>
<%= %>
The visibility of the inner markup is dependent on the result of the second statement, which with a VARQuantity of 5 will be False, causing the contents to be hidden.