Using conditional statements in Module Layouts
Module layouts can make use of conditional statements, which add an additional level of control and flexibility. Conditional statements contain the word "if":
"If (something) is the case, then (something)."
Accrisoft Freedom allows you to put these types of statements, along with HTML, into module layouts.
HTML-style <if> and <else> tags are used to make conditional statements in Freedom. Below is an example of a very simple conditional statment:
In this example from a News Articles detail layout, the header text will only display if the item's category is "Press Releases." Note the </if> tag to close the statement.
Here is an example of the same statement incorporating the <else> tag:
In this case, if the article's category is not "Press Releases," the header "General News" will display instead. Please note that there is no </else> tag, that the </if> tag closes the entire statement.
Certain operators can be used as part of conditional statements. The basic operators available are:
- And (&)
- Or (|)
- Equals (=)
- Does not equal (!=)
- Greater than (>)
- Greater than or equal to(>=)
- Less than (<)
- Less than or equal to (<=)
These operators work like in the original example: <if [[category]]='Press Releases'>. Note that the value, in this case Press Releases, is in single quotes.
In addition to simple operators, you can use mathematical operators
- Modulus Operator, which computes a remainder (%)
- Multiplication (*)
- Division (/)
- Subtraction (-)
- Addition (+)
An example of using a mathematical operator is using the modulus operator to determine if the row in a lister is even or odd so you can alternate the background color:
If it's an even numbered row, the row number divided by two will have a remainder of zero. Otherwise, it won't. Setting a different background color for the .even and .odd classes in the stylesheet will let you display alternating row colors on the lister.
A Practical Example
One common use of conditional statements is to display information only if it exists in Blue. For example, there are many fields you can fill out in Blue when adding a news article, and most of them aren't required. If you set up the layout to display the article's author like this:
and a certain news article doesn't have the Author field filled out, on the live website you'll just see "Author:" with a blank space after it:
However, you can use a conditional statement to display the Author line only if there has been something entered into the Author field:
Now, when any news article displays, the text "Author:" will only display if an author has been added to that article. Otherwise, nothing from the above statement will display on the live website:
You could also use the <else> tag to display a message saying the author's name is unavailable: