# How can we help?

## Overview

Using `{if}`, `{else}`, and `{ifelse}` statements in your HTML, you can render content in a message conditionally, which is a powerful way to personalize messages with dynamic content.

## Salutations

For basic salutations such as Hello (First Name), an easy way to conditionally print a value is to use an `if` statement based on if a contact attribute exists, as shown below:

```
Hello{if $contact.first_name} {$contact.first_name}{/if},
```

In the case where there is a first name value in the database, it will output:

```
Hello Michael,
```

And in the case when the contact's first name is not available, it will simply output:

```
Hello,
```

One possible variation could be that you want to print Hello Michael if the name exists in the database—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 a first name value, it will output:

```
Hello Friend,
```

### Default to Friend or other greeting

In addition to the above `{if}` statement methods, you can render default content using the default variable modifier. The following code will render the default value Friend if no value for first name exists.

```
Hello {$contact.first_name|default:'Friend'},
```

## Content based on gender or other contact attribute

You can print a block of content in a message based on a contact attribute value such as gender. For the following example, let's assume that gender is an attribute that stores `male`, `female`, or `null` if unknown. We're going to print one of two ads based on each contact's 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'`).

## Affinity content based on a contact's list value

You can print affinity content based on if a contact belongs to a specific list. In this example, we have a condition that the contact is a member of the `denim-enthusiast` list.

```
{if in_array('denim-enthusiast', $contact.lists)}
  Content for contacts in the 'denim-enthusiast' list here.
{/if}
```

When using the `in_array` function, you must use the above syntax because the `in_array` _modifier_ syntax is not supported.

### Supported

```
{if in_array($needle,$haystack}
```

### Not supported

```
{if $needle|in_array:$haystack}
```

## Comparison operators

Comparison operators is another way to render conditional content. You can also combine comparison operators for more powerful use cases.

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 for dynamic content

You can combine operators to further define conditional statements and render content based on if the contact meets specified criteria such as gender and age. The following example loads different content if the contact is a man, woman, or not of drinking age in the US.

Combine 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

```
{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 conjunction 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, rendering 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 because the `in_array` _modifier_ syntax is not supported.

### Supported

```
{if in_array($needle,$haystack}
```

### Not supported

```
{if $needle|in_array:$haystack}
```

- [Learn more about `{if}` statements on the Smarty documentation site.](http://www.smarty.net/docsv2/en/language.function.if)
