## If

Templating System : Designer Guide : Tag Reference : If

### Summary

The `if` tag is used to output a template section only when certain conditions are met.

### Usage Examples

    <if @x@ eq 5>True</if>
    <if @x@ eq "Greta">True</if>

    <if @x@ ne 5>True</if>
    <if @x@ ne "Greta">True</if>

    <if @x@ lt 5>True</if>
    <if @x@ le 5>True</if>

    <if @x@ gt 5>True</if>
    <if @x@ ge 5>True</if>

    <if @x@ odd>True</if>
    <if @x@ even>True</if>

    <if @x@ between 3 6>True</if>
    <if @x@ not between 3 6>True</if>

    <if @x@ eq 5 and @y@ eq 2>True</if>
    <if @x@ ge 5 or @y@ le 2>True</if>

    <if @s@ nil>True</if>
    <if @s@ not nil>True</if>

    <if @z@ in "Greta" "Fred" "Sam">True</if>
    <if @z@ not in "Greta" "Fred" "Sam">True</if>

### Expression Syntax

The condition of the <if> tag is built from terms of the form

> x0 [`not`] `op` x1 x2 ...

The operator `op` determines the number operands (x0, ... xn-1).

The following operators are available:

- binary
  - x0 `gt` x1
  - x0 `ge` x1
  - x0 `lt` x1
  - x0 `le` x1
  - x0 `eq` x1
  - x0 `ne` x1
- n-ary
  - x0 `in` x1 x2 x3 ...
- ternary
  - x0 `between` x1 x2
- unary
  - x0 `nil`
  - x0 `defined`
  - x0 `odd`
  - x0 `even`
  - x0 `true`
  - x0 `false`

Any of these operators can be prefixed with `not` to invert the outcome.

### Notes

- Any legal variables that may be referenced in the template may also be used in `if` statements. Words not surrounded with the commerical at sign (`@`) are interpreted literally.

- Phrases with spaces in them must be enclosed in quotes to be grouped correctly:

        <if @datasource.variable@ eq "blue sky">
          <td bgcolor=#0000ff>
        </if>

- The `elseif` tag may be used following an `if` block to specify an alternate conditional template section.

        <if @datasource.variable@ eq "blue">
          <td bgcolor=#0000ff>
        </if>
        <elseif @datasource.variable@ eq "red">
          <td bgcolor=red>
        </elseif>
        <else>
          <td bgcolor=#ffffff>
        </else>

- The `else` tag may be used following an `if` block to specify an alternate template section when a condition is not true:

        <if @datasource.variable@ eq "blue">
          <td bgcolor=#0000ff>
        </if>
        <else>
          <td bgcolor=#ffffff>
        </else>

- Compound expressions can be created by combining terms with the `and` and `or` keywords, as illustrated above. Any number of statements may be connected in this fashion. There is no way to group statements to change the order of evaluation.

- When a variable is tested using the `nil` operator, it will return true if the variable is undefined or if the value of the variable is an empty string.

templating@arsdigita.com