|
Synopsis
Discus allows a great degree of flexibility with IF-THEN-ELSE processing, allowing the template to make decisions as it is interpreted and display the corresponding result to the user. Whereas previous versions of Discus featured templates with a great degree of flexibility in substituting variables, conditional processing makes the possibilities of what can be done within templates nearly limitless.
There are two types of conditional processing available: block processing and in-line processing. Block processing allows more than one line to be included in the resulting text, or allows commands to be run. In-line processing allows a single decision to be made within a line of text. While it is possible to achieve anything desired with in-line processing by using block processing, the purpose of in-line processing is to make templates simpler.
Block IF-THEN-ELSE processing
The general structure of a conditional block is as follows:
<#if condition#>
Statements if true
<#else#>
Statements if false
<#endif#>
The <#else#> and statements if false are optional (i.e., you can set up a block that does something if the condition is true but does not do anything if the condition is false).
It is also possible to nest blocks.
<#if condition1#>
<#if condition2#>
1 = true, 2 = true
<#else#>
1 = true, 2 = false
<#endif#>
<#else#>
<#if condition2#>
1 = false, 2 = true
<#else#>
1 = false, 2 = false
<#endif#>
<#endif#>
Note that it is not necessary to indent lines within your conditional blocks (in fact, since Discus strips all leading spaces, it is irrelevant whether or not you have indented, and your indentation will be discarded from the final output anyway). However, it is easier to debug and maintain code that uses indentation to set apart the blocks.
There is currently not an equivalent of the Perl "elsif" function. The equivalent result can now be obtained by nesting the second and subsequent conditions within the ELSE block of the first condition.
In-line IF-THEN-ELSE processing
In-line processing is designed as a shortcut to make templates more compact, recognizing that often a single decision must be made within a line, and the display will not differ significantly if the condition is true or false. You may use a maximum of ONE in-line condition per line, and there are a number of commands that cannot be included within an in-line statement. It is possible to use a conditional statement within a line of text in any of the following ways:
1. <#if condition#>The condition is true!
2. <#if condition#>The condition is true! <#endif#> (Extra Text)
3. <#if condition#>It's true<#else#>It's false<#endif#> (Extra Text)
If the condition is true, the above code gives the following output:
1. The condition is true!
2. The condition is true! (Extra Text)
3. It's true (Extra Text)
If the condition is false, the above code gives the following output:
1.
2. (Extra Text)
3. It's false (Extra Text)
Note that in the above examples, the numbers 1 through 3 and the period (.) that followed them was text at the beginning of the line, which was reproduced onto the screen whether or not the condition was false. Examples 2 and 3 had text placed after <#endif#> which was also displayed regardless of whether the condition was true. If it turns out that there would be no text whatsoever on a line (presumably because a condition was false), then the blank line will be ignored in the output.
Since experience has shown that in-line conditional statements also occur within HTML tags, it is possible to use { and } rather than < and > when writing in-line conditional statements. This is the only place where this replacement is valid (in general, you cannot replace <#...#> with {#...#} and expect it to work). The following code presents a checkbox, and checks it by default if the condition is true.
<input type=checkbox name="test" value=1{#if condition#} checked{#endif#}> My box
Conditions
A number of conditions can be used in both the block and in-line methods. One special method tests whether or not you have Discus Pro:
<#if pro#>Thanks for your purchase<#else#>Please send us money<#endif#>
<#if ! pro#>This nag text will disappear when you do
If you have Discus Pro:
Thanks for your purchase
If you do not have Discus Pro:
Please send us money
This nag text will disappear when you do
The other tests compare variables and values in a variety of ways.
{#if $var1 == $var2#}var1 and var2 are equal numerically
{#if $var1 != $var2#}var1 and var2 are not equal numerically
{#if $var1 > $var2#}var1 is numerically greater than var2
{#if $var1 >= $var2#}var1 is numerically greater than or equal to var2
{#if $var1 <= $var2#}var1 is numerically less than or equal to var2
{#if $var1 < $var2#}var1 is numerically less than var2
{#if $var1 eq $var2#}var1 is textually equal to var2
{#if $var1 ne $var2#}var1 is textually not equal to var2
{#if $var1 gt $var2#}var1 is textually greater than var2
{#if $var1 ge $var2#}var1 is textually greater than or equal to var2
{#if $var1 le $var2#}var1 is textually less than or equal to to var2
{#if $var1 lt $var2#}var1 is textually less than var2
The above results behave as you would expect their Perl counterparts to behave. You can also use any variable substitution method (e.g., hash value), or just type in a number or a string instead of using a variable. A practical example is:
{#if $general->{value} == 1#}Value is ONE{#else#}Value is $general->{value}{#endif#}
I have $general->{apples} apple{#if $general->{apples} != 1#}s{#endif#}
There is one difference between Perl and the Discus template interpreter that involves the == operator. In Perl, the condition "Discus" == "UBB" would be true, since "Discus" and "UBB" each have a numerical value of 0, and 0 is numerically equal to 0. In the Discus template language, if the first comparison contains any non-digits and if both the first and second comparison evaluate numerically to 0, then the result is false. Careful coders should not be affected by this; the Discus interpretation of the == sign is designed to protect against inexperienced people not appreciating the difference between == (for numerical comparisons) and eq (for text comparisons).
Other Conditions
Some other operations in the conditions are possible in addition to the operators that resemble the equivalent Perl operations. Pattern matching is possible:
1. {#if $variable =~ match(text)#}variable contains "text"
2. {#if $variable !~ match(text)#}variable does not contain "text"
3. {#if $variable rexp=~ match(\d)#}variable contains a digit
4. {#if $variable rexp!~ match(\d)#}variable does not contain a digit
The first two statements allow simple text matching and do not use regular expressions (if, in statement number 1, you used "\d", the condition would be true if the variable contained the text "\d", regardless of whether the variable contained a digit). The last two statements treat your text as a regular expression. The template will fail if your regular expression is not valid.
It is possible to perform a quick mathematical computation.
{#if [ $variable1 + $variable2 ] == 2#}Sum is 2
{#if [ $variable % 2 ] == 0#}Variable is even
You may use addition (+), subtraction (-), multiplication (*), division (/), and modulus (%) in your expression. You can perform an operation on ONLY TWO VARIABIES in your quick computation and you must surround your computation with the square brackets. If you divide by 0, the expression will evaluate to 0 (there will not be an error message).
You can test the existence of a file, or test whether a global option is defined. Note that testing if an option is defined is much different than testing if the option is equal to 0.
With the new Discus 4.0 Options Manager, testing whether an option is defined is needed only for backward compatibility.
{#if exists:"$admin_dir/discus.conf"#}Your discus.conf exists (good!)
{#if option_defined:"profanity"#}You've chosen whether or not to use the profanity filter
AND and OR in conditions
It is possible to use EITHER && (and) or || (or) within your conditions to make two or more comparisons at once. You cannot mix these (use && and || in the same condition). Parentheses surrounding conditions are not recognized.
{#if $var1 == 2 || $var2 == 3#}var1 is 2 or var2 is 3
{#if $var1 == 2 && $var2 == 3#}var1 is 2 and var2 is 3
{#if $var1 == 2 && $var2 == 3 && [$var1 + $var2] == 4#}This can't be true
|