In How to pull draw information from our API we talked about getting variables from the API response using Liquid’s dot notation.
What if you want to get a specific item from an array (list of many items)?
If you want to do any of the following things you would use a for loop:
- Retrieve data for a specific lottery or a specific draw number
- Loop over all of your products to find the one with the highest price
- Output a list of all currently available lotteries
We know that offers
is an array (a list of related items) because its value starts and ends in square brackets ([]
).
In this example from the previous article, we grabbed the first offer from the offers array:
{{content_blocks.${pricing}}}
{{pricing.result.offers.first}}
- Initiate the content block
- Use dot notation to traverse your way down the response and output a value
But what if we didn’t want the first offer in the above array? What if, for example, we wanted Oz Lotto instead of Powerball?
This is a good example of when you would use a for loop.
How To Use A For Loop
1. Assign The Entire Offers Array As A Variable
First, assign the entire offers array as a variable:
{{content_blocks.${pricing}}}
{% assign offers = pricing.result.offers %}
2. Add A ‘For Loop’
Now add a ‘for loop’ to loop over all the objects within that array to find the one we want. Objects start and end with curly brackets ({}).
{{content_blocks.${pricing}}}
{% assign offers = pricing.result.offers %}
{% for i in offers %}
{% if i.key = "oz_lotto" %}
{% assign offer = i %}
{% break %}
{% endif %}
{% endfor %}
The offer we want is: {{offer}}
Let’s Break Down The Above Code Some More…
The code loops over all of the offers, from start to finish, until it finds the offer we want.
With each iteration of the loop, we assign the current offer as a variable called “i”, as per this first line.
{% for i in offers %}
If the current offer (i) has a key
property with the value we are looking for ("oz_lotto"
)
{% if i.key = "oz_lotto" %}
…then we have found a matching offer. We save this as a new variable called “offer“
{% assign offer = i %}
…and we stop searching.
{% break %}
We now have the entire Oz Lotto offer saved as a new variable called {{offer}}
.
Remember How We Talked About Finding Repeating Patterns And Fixing Them?
Creating for loops in all of your emails will become quite cumbersome and repetitive.
Instead of writing for loops in all your messages, you can do it once in our pricing content block and then assign what you are looking for as a variable.
{% assign offers = pricing.result.offers %}
{% for i in offers %}
{% if i.key = {{ lottery_key }} %}
{% assign offer = i %}
{% break %}
{% endif %}
{% endfor %}
You’ll notice that we’ve removed “oz_lotto” in the above example and instead use a lottery_key
variable.
You’ll also notice that lottery_key
was not assigned in this content block.
That’s because not only can you assign variables in your content block but you can also read variables set earlier in your message.
For example, we assign the lottery_key
variable at the start of their email before the pricing and branding content blocks are each initiated. Each of those content blocks can read the value of the lottery_key
variable so now each variable we assign based on the value of {{offer}}
will be for the exact offer we want.
{% assign lottery_key = "oz_lotto" %}
{{content_blocks.${pricing}}}
{{content_blocks.${branding}}}
<div style="background:{{primary_colour}}"> <img src="{{logo}}" />
Hi {{${first_name}}},
You might be interested in this Oz Lotto draw closing soon:
Jackpot image: {{jackpot_image}}
Draw closes: {{draw_stop}}
Prize pool: {{prize_pool}}
Click here to enter: https://api.ozlotteries.com/{url}}
You can also assign a draw_number and have the pricing content block loop over the draws to find one with a matching number and then assign that as a new variable called draw
.