Skip to main content

Loops in messages

The message supports loops for JSON variables of type "list" [] and "object" {}, as well as access to an object element by key {json.object.name}.

If additional profile fields are inserted as variables into the template, fields of the "Array of Objects" and "Tag" (list of strings) types can also be used in loops.

Nested loops are also possible.

Variables example

{
"Addresses": ["Barclay street, 244 building", "Green street, 555 building"],
"Information": {
"LJ": [
{ "name": "Optimyss X1", "price": "$10" },
{ "name": "Optimyss X2", "price": "$20" }
],
"Samsung": [{ "name": "Galaktiko 17", "price": "$50" }]
}
}

Loops example

<!-- Loop by object elements -->
{for $vendor $models = json.Information}
<p>{$vendor}</p>

<ol>
<!-- Nested loop -->
{for $index $model = $models}

<!-- Call elements by key -->
<li>{$model.name} {$model.price}</li>

{else}
<p>Empty array!</p>
{end}
</ol>

{else}
<p>Empty array!</p>
{end}


<h5>Addresses:</h5>
<ol>
<!-- Simple loop by strings list -->
{for $index $address = json.Addresses}
<li>{$address}</li>
{else}
<p>Empty array!</p>
{end}
</ol>

Result

<h5>New:</h5>

<!-- Loop by object elements -->
<p>LJ<p>
<ol>
<!-- Nested loop. Call elements by key -->
<li>Optimyss X1 $10</li>
<li>Optimyss X2 $20</li>
<ol>

<!-- Loop by object elements -->
<p>Samsyng<p>
<ol>
<!-- Nested loop. Call elements by key -->
<li>Galaktiko 17 $50</li>
<ol>

<h5>Addresses:</h5>
<ol>
<!-- Simple loop by strings list -->
<li>Barclay street, 244 building</li>
<li>Green street, 555 building</li>
</ol>