# Conditional blocks

## Syntax

Conditional blocks in XSSI look as follows:

       <!-- #if expr="test_expr_1" -->
               Content to process if test_expr_1 is true
       <!-- #elif expr="test_expr_2" -->
               Content to process if test_expr_2 is true
               .
               .
       <!-- #elif expr="test_expr_n" -->
       		Content to process if test_expr_n is true
       <!-- #else -->
       		Content to process if all the above tests are false
       <!-- #endif -->

`#elif` sections are optional. A conditional block can have zero or many of them. The `#else` is optional and a conditional block can have either no `#else` or exactly one.

Conditional blocks can be nested.

## Expression evaluation

The value of the `expr` attribute inside `#if` and `#elif` directives is considered as an expression which evaluates to true or false according to these rules:

string

  Evaluates to true if string is not empty.

string_1 `<` string_2, string_1 `<=` string_2, string_1 `>` string_2, string_1 `>=` string_2

  Performs a lexicographic comparison between string_1 and string_2.

string_1 `=` string_2, string_1 `==` string_2, string_1 `!=` string_2

  Tests the equality (with `=` or `==`) or inequality (with `!=`) of string_1 and string_2.

  If string_2 has the form `/`regex`/`, a regular expression matching operation will take place instead. Any literal slash character (`/`) in regex has to be escaped by preceding it with a backslash character (`\`).

  Matched sub-strings are captured inside the variables `$0`, `$1`, ..., `$9`. For more information about backreferences, refer to the "Regular Expressions" appendix.

test_expr_1 `&&` test_expr_2

  Evaluates to true if both test_expr_1 and test_expr_2 expressions are true.

test_expr_1 `||` test_expr_2

  Evaluates to true if at least one of the expressions test_expr_1 and test_expr_2 is true.

`!` test_expr

  Evaluates to true if test_expr is false.

`(` test_expr `)`

  Evaluates to true if test_expr is true. Parentheses are used to enclose sub-expressions and supersede the priorities of their operators.

`!` has higher priority than `&&` and `||` which at their turn have higher priority than `=`, `!=`, `==`, `<=`, `>=`, `<`, and `>`.

Example 7-2. Variable Expansion in Expressions

If a string operand (such as string, string_1 or string_2 in the above syntax list) includes a variable expansion that may contain a sequence similar to one of the operators, it is recommended to enclose it between quotes to avoid any ambiguity.

For example, if variable `HTTP_COOKIE` is set to `lang=en`, the following directive will fail with an error because the expansion of the variable includes the character `=` which is considered as an equality test:

       <!-- #if expr="$HTTP_COOKIE = /lang=(.*)/" -->
       	Language code is <!-- #echo text="$1" -->
       <!-- #else -->
       	No language is set.
       <!-- #endif -->

The proper way to do the test without errors is to enclose the expansion between quotes as in :

       <!-- #if expr="'$HTTP_COOKIE' = /lang=(.*)/" -->
       	Language code is <!-- #echo text="$1" -->
       <!-- #else -->
       	No language is set.
       <!-- #endif -->