How can we help?

Randomize content


Using Smarty, you can randomize blocks of content in a message. The content will be randomized at send time or during message preview. This is useful if you need to display advertising content randomly in a message.


In this first example, we'll assign a variable with a random value between 1 and 4. Then in the content, we can use an {elseif} statement to print the random result:

{$data.min = 1}
{$data.max = 4}
{$data.random = "{math equation='rand(min, max)' min=$data.min max=$data.max}"} {if $data.random == 1} Ad 1 {elseif $data.random == 2} Ad 2 {elseif $data.random == 3} Ad 3 {else} Ad 4 {/if}
<h1>Debug Data</h1> {$utils->jsonPrettyPrint($data)}

Every time the message is sent or previewed, the content will change according to the number that was selected at random by Smarty.

We can also render a random value from an array:

{* Math for Random *}
{$random.min = 0}
{$random.max = 3}
{$random.index = "{math equation='rand(min, max)' min=$random.min max=$random.max}"}
{* Array *} {$array_list = ['value1','value2','value3','value4']}
{* Return a Random Number Between 0-3 for the Array *} {$random_result = $array_list[$random.index]} <p>{$random_result}</p>

In the next article, you can learn about calculating distance between postal codes.



Please sign in to leave a comment.