## April 01, 2007

Conditions, Calculations and Variables

## Conditional Tags

[if], [ifn], [iff], and [iffn].

[if] tests a single variable. If that variable exists and has a true (non-zero / non-null / non-empty) value, the template code within the bracket of the [if] and the [/if] is evaluated.

If the variable is false (zero / null / empty), the code is not evaluated.

And if the variable does not exist, the [if] tag itself is not evaluated, and is instead included directly in the output. So if you get the name of the variable wrong, and type [if post.coments] instead of [if post.comments], you will be able to see your mistake right there in the page. In this case, the bracketed code is processed as if the [if] tag did not exist.

[ifn] works exactly the same, except that the condition is reversed. Code inside the [ifn] [/ifn] is evaluated if the variable is false.

With [if] and [ifn], if the variable does not exist the behaviour of a following [else] statement is undefined.

[iff] and [iffn] are similar, with one big exception: The bracketed code is evaluated if and only if the variable evaluates to true (or false, for [iffn]). If the variable does not exist, the code is skipped, just as if the variable had been false.

## Comparison Tags

[if.eq], [if.ne], [if.gt], [if.lt], [if.ge], [if.le]

These extended conditional tags compare two values, to see if, respectively, they are equal or unequal, or that the first value is greater than, less than, greater than or equal, or less than or equal to the second value. They must be closed by a corresponding [/if.XX] tag such as [/if.eq], not by a [/if].

The values can be either variables or constants. The way Minx determines which is the case is somewhat simplistic: If a variable exists which has the name of the value, it assumes that's what you mean; if not, then it assumes the value is a constant. So unlike the simple [if] tag, there's never a case where the variable doesn't exist.

These tags will perform integer comparisons if both values are integers; otherwise they will perform string comparisons. So [if.gt 10 2] will evaluate as true, just as you'd expect, and [if.gt a10 a2] will evaluate as false.

[I had held off on implementing these for the sake of keeping the template language simple, but it turns out I need them to properly manage context-sensitive selection lists - such as on my new forum pages - so in they went.]

These tags also work if supplied with a single variable. The missing variable is assumed to be zero, if the provided variable is a number; False, if the value is True, blank, if the value is a string, and None otherwise.

## Extended Comparison and Conditional Tags

[if.z], [if.nz], [if.odd], [if.even]

These codes take a single variable.

[if.z] and [if.nz] test whether the value is zero or non-zero. This is effectively identical to using [if.eq] and [if.ne] with a single numeric variable, but may be clearer to the user.

[if.odd] and [if.even] test whether the value is odd or even. If the value is not numeric, the result is undefined.

[if.all], [if.any], [if.one], [if.none]

These codes currently take two variables, but may be extended in the future to take an arbitrary number of values.

[if.all] tests whether both the variables are true (non zero / non-empty) - effectively an and.
[if.any] tests whether either or both of the variables is true - an or.
[if.one] tests if one and only one of the variables is true.
[if.none] tests if neither of the variables is true - a nor.

For all comparison tags and extended conditionals, there is also a matching [ifn.condition], which simply reverses the result.

## Else

Code within [else]....[/else] is evaluated if the last tried conditional block within the current template was not evaluated. In other words, it works the way you would expect, with one addition: You can have multiple [else] blocks after one [if] block, and all the [else] blocks will be evaluated if the [if] fails.

## Variables and Calculations

[set]

You can set a variable with the [set] tag. Like the extended conditionals, [set] can take either another variable or a constant, so [set a post.title] or [set a "Wombats in Paradise"] are valid examples. Again like the extended conditionals, [set] assumes a variable if the variable name exists, and a constant otherwise.

The variable can be accessed again with the prefix var. So [var.a] will print the value of a, and [if var.a] will test it. You can use the var.a terminology inside set as well, and it will be interpreted appropriately.

Variables can be used anywhere a normal data tag could be used.

[calc.add], [calc.sub], [calc.mul], [calc.div], [calc.mod], [calc.min], [calc.max]

The [calc] tag performs a calculation based on two values, and assigns the result to a variable. The values must be integer variables or constants; anything else will be evaluated as zero.

So [calc.add x var.y var.z] will set the variable x to be the sum of y and z.

[inc], [dec]

Just want to increment or decrement a counter? These are the tags for you! [inc a] or [dec a] and you're done.

If the variable did not exist beforehand, it is created as zero, and then incremented or decremented.

Note that none of these tags can alter the value of an existing data tag, only user variables.