Skip to main content

if

Video tutorial

if

if constructs allow you to display content if specified conditions are met.

if <emExpression</em> <emContent</em>

Content is only displayed if Expression is true. Expression is evaluated to a boolean logical value (TRUE or FALSE). If Expression is evaluated to TRUE, then Content is displayed. If not, Content is ignored.

At its most basic, an if construct consists of only one condition that is introduced using the expression if. The content that follows it is displayed or a function is called.

In single-line code blocks, if constructs must end with the expression endif.

{% if $_a > $_b %}
	a is greater than b
{% endif %}

Alternative

{%
	if($_a > $_b)
	{
 		print("a is greater than b");
	}
%}

else

It is often necessary to run one statement if a specific condition is met and a different statement if that condition is not met. In these cases, else is used. else extends if statements to include an additional statement that is run if the expression within the if statement is evaluated as FALSE. The following programming code returns a is greater than b if the value of$ais greater than the value of$_b. Otherwise, it returns _a is NOT greater than b.

{% if $_a > $_b %}
	a is greater than b
{% else %}
	a is NOT greater than b
{% endif %}

Alternative

{%
	if($_a > $_b)
 	{
		print("a is greater than b");
	}
	else
	{
		print("a is NOT greater than b");
 	}
%}

The else statement is only run when the if expression is evaluated as FALSE.

elseif

elseif is a combination of if and else. Comparable to else, the expression elseif extends an if construct to display alternative content if the original if condition is not met. However, in contrast to else, the alternative content is only displayed if the elseif condition is met. The following example code returns a is greater than b, a equals b or a is less than b:

{% if $_a > $_b %}
	a is greater than b
{% elseif $_a == $_b %}
	a equals b
{% else %}
	a is less than b
{% endif %}

Alternative

{%
	if($_a > $_b)
	{
 		print("a is greater than b");
	}
	elseif($_a == $_b)
	{
 		print("a equals b");
	}
	else
	{
 		print("a is less than b");
	}
%}

Several elseif structures can be used within a single if control structure. The first structure that meets the condition is run.

The elseif part is only run if the previous if condition and all other previous elseif conditions do not apply (FALSE) and if the current elseif condition applies (TRUE).

contains

The contains condition was implemented to take account of specific use cases. With this condition, contents which are contained in a string are displayed. This can also be the case if several individual conditions apply. In such cases, the contains condition allows for simplified notation.

In the following example, payment data is only displayed if $MethodOfPaymentID is contained in the string "1|2|3", i.e. is equivalent to the values 1, 2 or 3. The template variable $MethodOfPaymentID only outputs numbers. This code can be used in email templates.

{% if "1|2|3" contains $MethodOfPaymentID %}
	Payment information for cash on delivery (ID 1), invoice (ID 2) and debit (ID 3)
{% endif %}

Complex expressions

Expressions can also consist of several conditions linked by a logical operator (*&&, || *).

If different conditions are to be grouped together, brackets must be used. Conditions within brackets are evaluated as a group and the result (TRUE or FALSE) is evaluated with the additional conditions.

In the following example, one of the conditions within the brackets must be met. At the same time, $_d must contain the boolean value TRUE. In this example, it is also sufficient that $_b has the value 1 and $_d has the value TRUE.

{% if ( $_a &gt; 0 || $_b &gt; 0 || $_c &gt; 0) &amp;&amp; $_d == TRUE %}
	Content
{% endif %}

Alternative

{%
	if(( $_a > 0 || $_b > 0 || $_c > 0) && $_d == TRUE)
	{
		print("Content");
	}
%}

In the next example the content is displayed both if the value of $_a, $_b or $_c is greater than 0 and if the value of two or all is greater than 0.

{% if $_a &gt; 0 || $_b &gt; 0 || $_c &gt; 0 &amp;&amp; $_d == TRUE %}
	Content
{% endif %}

Alternative

{%
	if($_a &gt; 0 || $_b &gt; 0 || $_c &gt; 0 &amp;&amp; $_d == TRUE)
 	{
		print("Content");
	}
%}

This means that when defining complex expressions, you need to understand the dependencies that exist for the individual conditions to achieve the desired result.

To top