Skip to content

Conditions

Craig edited this page Nov 17, 2017 · 7 revisions

Conditional if statements are used to perform different actions based on different conditions. The code within an if block will be only rendered if the condition evaluates to true. The syntax for using an if statement is:

{{if condition}
	block of code to be executed if the condition is true
{{/if}}

For example:

{{if $.Active}}
	Currently Active
{{/if}}

Nested if statements are also supported:

{{if $.Active}}
	Currently Active
	
	{{if $.HasProfile}}
		The user has a profile
	{{/if}}
{{/if}}

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax

{{if condition}}
	block of code to be executed if the condition is true
{{else}}
	block of code to be executed if the condition is false
{{/if}

Example

If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":

{{var hour = 10}}

{{if hour < 18}}
	Good day
{{else}}
	Good evening
{{/if}}

The result of greeting will be:

Good day

The else if Statement

Use the else if statement to specify a new condition if the first condition is false.

Syntax

{{if condition1}}
	block of code to be executed if condition1 is true
{{else if condition2}}
	block of code to be executed if the condition1 is false and condition2 is true
{{else}}
	block of code to be executed if the condition1 is false and condition2 is false
{{/if}}

Example

If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening":

{{var time = 10}}

{{if time < 10}}
	Good morning
{{else if time < 18}}
	Good day
{{else}}
	Good evening
{{/if}}

The result of greeting will be:

Good day

Boolean Expressions

If statements use Boolean expressions to evaluate the condition. A Boolean expression is an expression which evaluates to either true or false. Nested expressions are supported through sub-bracketing. An expression can also be assigned to a variable. See variables for more information.

The expression is broken down into one or more conditions joined by operators. A condition can be either a single value that evaluates to true or false, or a comparison of two values. The following operators are supported:

Operator Description
== Equality check. Ensures the values on the left and right side of the condition are equal.
!= Inequality check. Ensures the values on the left and right side of the condition are not equal.
> Greater than. Ensures the value on the left side of the condition is greater than the value on the right side.
>= Greater than or equal to. Ensures the value on the left side of the condition is greater than or equal to the value on the right side.
< Less than. Ensures the value on the left side of the condition is less than the value on the right side.
<= Less than or equal to. Ensures the value on the left side of the condition is less than or equal to the value on the right side.
& Logical AND. Ensures the values on the left AND right side of the condition both evaluate to true.
| Logical OR. Ensures either the left side OR right side of the condition evaluates to true.

The following example demonstrates a more complex Boolean expression:

{{var available = true}}
{{var number1 = 10}}
{{var number2 = 15}}
{{var name1 = "Craig"}}
{{var name2 = "John"}}

{{if available & (number2 > number1) | (name1 != name2)}}
	Success
{{/if}}

In this example, "Success" would be rendered because available is true and number2 is greater than number1. In this case it doesn't matter that name1 is not equal to name2 as we have used an OR operator to join the two sub-expressions.

Conditional Bindings

The conditional binding block returns one of two values depending on the value of a Boolean expression. The syntax for using a conditional binding is {{=(Condition) ? TrueValue : FalseValue}} where Condition is a Boolean expression and TrueValue or FalseValue can be any Nettle data type.

For example:

{{= ($.LoginCount > 0) ? "Welcome back" : "Welcome first time user" }}

Which (assuming LoginCount is 0) would generate the output:

Welcome first time user

The condition must evaluate to true or false. If condition is true, TrueValue is evaluated and becomes the result. If condition is false, FalseValue is evaluated and becomes the result. Only one of the two expressions is evaluated.

The conditional binding is similar to the C# conditional operator.

Clone this wiki locally