Overview
Using {if}, {else} and {ifelse} statements, you are able to render content in a message conditionally.
Render content based on if a contact attribute exists
In the case of a salutation (e.g. "Hello Michael, versus Hello"), an easy way to conditionally print a value is to use an "if" statement like the following:
Hello{if $contact.first_name} {$contact.first_name}{/if},
In the case where there is a first name value, it will output:
Hello Michael,
And in the case where there is not, it will simply output:
Hello,
A variation may be that you want to print "Hello Michael," if the name exists or "Hello Friend," if it does not. To do that, simply change the Smarty code to the following:
Hello{if $contact.first_name} {$contact.first_name}{else} Friend{/if},
In the case where there is a first name value, it will output:
Hello Michael,
And in the case where there is not, it will output:
Hello Friend,
Use the default variable modifier
In addition to the above {if} statement methods, you are also able to render default content using the default variable modifier.
Hello {$contact.first_name|default:'Friend'},
The above code will render the default value Friend if no value for first name exists.
Render content based on a contact's attribute value
In some cases you will want to print a block of content conditionally based on a contact's attribute value. For example let's assume "gender" is an attribute that stores "male" or "female" or null if unknown. In this example, we are gong to print one of two ads based on gender.
{if $contact.gender == 'male'} Male targeted content here {elseif $contact.gender == 'female'} Female targeted content here {else} Default content here {/if}
When the value is a string, it must be wrapped in single quotes (i.e. 'male').
Render content based on a contact's list value
You may want to print content only if the contact belongs to a specific list. In this example, we have a condition that the contact is a member of the 'auto-enthusiast' list.
{if in_array('auto-enthusiast', $contact.lists)} Content for contacts in the 'auto-enthusiast' list here. {/if}
When using the in_array function, you must use the above syntax as the in_array modifier syntax is not supported.
Supported
{if in_array($needle,$haystack}
Not supported
{if $needle|in_array:$haystack}
Render content using comparison operators
You can use comparison operators to render conditional content.
Using the "equals" operator (==):
{if $contact.age == 18} You are 18 years old. {else} You are not 18 years old. {/if}
When the value is a number, it does not need to be wrapped in single quotes (i.e. 18).
Using the "does not equal" operator (!=):
{if $contact.age != 18} You are not 18 years old. {/if}
Using the "greater than" operator (>):
{if $contact.age > 18} You are old enough to vote. {else} You are too young to vote. {/if}
Combine comparison operators
You can combine operators to further define conditional statements.
Combining the "equals" operator (==) with the "greater than" operator (>) using "and" (&&):
{if $contact.gender == 'male' && $contact.age > 21} Grab yourself a beer. {elseif $contact.gender == 'female' && $contact.age > 21} Grab yourself a glass of wine. {else} You are too young to drink. {/if}
Adding the "or" condition (or):
{if $contact.gender == 'male' or $contact.gender == 'female' && $contact.age > 21} Grab yourself a beer or a glass of wine. {else} You are too young to drink. {/if}
Below is a list of comparison operators you can use in conjunciton with an "if" statement.
Operator | Meaning | Syntax Example |
---|---|---|
== | equals | {$a == $b} |
!= | not equal | {$a != $b} |
> | greater than | {$a > $b} |
< | less than | {$a < $b} |
>= | greater than or equal | {$a >= $b} |
<= | less than or equal | {$a <= $b} |
Render content based on if an item exists in an array
Contact attributes can store an array of items. Using Smarty, you can render content in a message based on if an item exists in a contact attribute array.
{if in_array(kiwi, $contact.fruits)} Kiwi is in the fruits array. {else} Kiwi is not in the fruits array. {/if}
The above code will check to see if "kiwi" exists in the "fruits" array attribute per contact and render different content accordingly.
You can check the array for multiple values using an "and" operator:
{if in_array(kiwi, $contact.fruits) && in_array(peach, $contact.fruits)} Kiwi AND Peach are in the fruits array. {else} Kiwi AND Peach are not in the fruits array. {/if}
Using an "or" operator:
{if in_array(kiwi, $contact.fruits) || in_array(peach, $contact.fruits)} Kiwi OR Peach is in the fruits array. {else} Kiwi OR Peach is not in the fruits array. {/if}
When using the in_array function, you must use the above syntax as the in_array modifier syntax is not supported.
Supported
{if in_array($needle,$haystack}
Not supported
{if $needle|in_array:$haystack}
In the next article, you can learn about {foreach} statements.
Comments
3 comments
Is it possible to use an IF statement with an external variable? How would the syntax look for that? For example:
{if $extVars.order.Totals.CreditsUsed}
${$extVars.order.Totals.CreditsUsed|string_format:"%.2f"}
{/if}
Hi Chris,
Yes, it is possible to use an IF statement with an external variable. The syntax is.
{if $extVars.variableName == 'value'}
{/if}
If there is a specific use you are trying to solve please consider filing a support ticket.
Thanks Brian!
Please sign in to leave a comment.