Conditional if/else Statements

During development of your merge workflows and templates, you may find instances where you want to alter the output of the variable, or even an entire template section, using a conditional statement. You can use conditional statements anywhere you call Smarty variables; in your document templates, settings fields, field maps, route rules, email and other…

Image Description

During development of your merge workflows and templates, you may find instances where you want to alter the output of the variable, or even an entire template section, using a conditional statement. You can use conditional statements anywhere you call Smarty variables; in your document templates, settings fields, field maps, route rules, email and other delivery configurations.

Below is a simple example of using an if statement in the field map of a route or document to transform the $isUpdate value from a “1” to “Yes”, only if the value is 1.

isUpdate{if $isUpdate == "1"}Yes{/if}


You can add elseif / else statements too.

isUpdate{if $isUpdate == "1"}Yes{else}No{/if}
or
isUpdate{if $isUpdate == "1"}Yes{elseif $isUpdate == "2"}Unknown{else}No{/if}


Sometimes you’ll want to include multiple evaluations in your if or elseif statements. You can use “and“, “or“.

_freeShipping{if $price > 50 and $state == "CA"}FREE{/if}


You can also group operations. For example:

_freeShipping{if ($price + $tax) > 50 and ($state == "CA" or $state == "TX")}FREE{/if}


You can also run statements within statements.

_freeShipping{if $price > 50}{if $state == "CA" or $state == "TX"}FREE{/if}{/if}


And you can use Modifiers within statements.

productNote{$productNote}{if stristr($name|substr:0:10, "Red"} Color varies slightly.{/if}


Within HTML templates, uploaded office document templates, and email deliveries, you can use statements to hide/reveal entire sections, tables, table rows, images, etc, based on the incoming data.

{if $color == "Red"}
If the value for “color” is “Red”, anything in this section will display.
{elseif $color == "Blue"}
This section will show if the value is “Blue”.
{else}
This section is shown for any other value, or no value.
{/if}


AVOID EMPTY LINES

To avoid empty lines, it’s best practice to wrap your statements around carriage returns you do not want displayed unless a section is triggered. In the below example, the two outputs vary in the displayed line breaks. Also, in the first example, there’s an additional space if {$address2} is empty.

CODEOUTPUT

{$companyName}
{if !empty($address1)}
{$address1}
{/if}
{if !empty($address2)}

{$address2}
{/if}
{if !empty($city) or !empty($state) or !empty($zipcode)}

{$city}{if !empty($state)}, {$state}{/if}{$zipcode}
{/if}

Joe’s Dozer

12 Main St.



Austin, TX 99999

{$companyName}{if !empty($address1)}
{$address1}{/if}{if !empty($address2)}
{$address2}{/if}{if !empty($city) or !empty($state) or !empty($zipcode)}
{$city}{if !empty($state)}, {$state}{/if}{$zipcode}{/if}

Joe’s Dozer
12 Main St.
Austin, TX 99999


HIDE TABLE AND LIST ROWS

You can hide table rows and list items using tableif and listif.

AddressCityStateZipcode
{tableif !empty($address1)}{$address1}{$city1}{$state1}{$zip1}{/tableif}
{tableif !empty($address2)}{$address2}{$city2}{$state2}{$zip2}{/tableif}
{tableif !empty($address3)}{$address3}{$city3}{$state3}{$zip3}{/tableif}
  • {listif !empty($address1)}Address 1: {$address1}, {$city1}, {$state1} {$zip1}{/listif}
  • {listif !empty($address2)}Address 2: {$address2}, {$city2}, {$state2} {$zip2}{/listif}
  • {listif !empty($address3)}Address 3: {$address3}, {$city3}, {$state3} {$zip3}{/listif}

For more on table and list rows, including looping through them, see Sorting and Looping Through Objects/Arrays.


HIDE TABLE ROWS ALTERNATIVE

There are cases where you may be working within a table in Word, Excel, or HTML where it may make sense to use if, elseif statements to show or hide rows as well. When doing so, unless you have a scoped reason to do otherwise and an clear understanding of your table’s columns/rows layout, you should always open and end your {if …}, {elseif …}, and {/if} in the same column. Typically this would occur at the end of table rows, although in some cases it may make sense in the starting row.

{$name}{if !empty($address1)}
{$address1}
{$city1}{$state1}{$zip1}{elseif !empty($address2)}
{$address2}
{$city2}{$state2}{$zip2}{/if}


CHECKBOXES

Checkboxes can be used in HTML and office document templates. To show pre-selected checkboxes, you could accomplish this in a few ways:

{if $answer == "No"}[X] No [  ] Yes{else}[  ] No [X] Yes{/if}

{if $answer == "No"}☒ No ☐ Yes{else}☐ No ☒ Yes{/if}

{if $answer == "No"}{else}{/if} No
{if $answer == "Yes"}{else}{/if} Yes
{if $answer == "No Answer"}{else}{/if} No Answer

Tags: , , , ,

Was this article helpful to you?

Yes No

Related Articles