Liquid Tutorial: How To Add All Items In An Array

“Way to go! You’ve meditated for a total of 325 minutes last month!”
We love to see recap posts like this, but how can we easily and automatically obtain a total sum of our data?
Watch the full video to learn how to add all items in an array!
🗣️ WATCH FULL YOUTUBE VIDEO HERE 🗣
What’s up everyone, my name is Allan, Founder and Consultant at For Now Marketing, and welcome back to our channel!
Before we get started, if you have any questions, whether you are currently using Braze or considering using Braze, please feel free to reach out! You can find these addresses and more in the Youtube description below.
Today, we are going to talk through How To Add All Items In An Array in Braze.
Let’s get started!
Data Prerequisite
Before we get started, our tutorial does assume that we’re working with an array type data that stores the duration of the user’s meditation sessions from the previous month.
Here’s a screenshot of what it looks like in the User Profile.

Also, you can see that this data has an array data type.

To be specific, this data is an array of integers or array of numbers.
previous_month_meditation_session_durations_mins = [26, 36, 41, 9, 23, 25, 19, 8, 12, 17, 37, 30, 29, 13]
This data here captures all the different meditation sessions that a user had last month, specifically the duration of each meditation session in minutes.
Although you may not have this exact data structure or this specific data around meditation duration, this tutorial is very transferrable to so many different use cases!
Rendering Array Data
First, let’s just see how this data renders when we simply insert the Liquid variable without any changes.
So in Braze, I’m going to look up my existing user, and whoa! We get one giant number, which is not what we want!
And if you look closely, this is actually all the numbers inside our array, just printed all at once, side by side.
So at least we know that the numbers render successfully, and now it’s just a matter of adding all of them.
But how do we do this?!
Liquid For Loops
So here’s the idea. We’re going to create a Liquid variable called “sum” which will ultimately have the sum total of all the minutes inside the array.
And we’re going to loop through the array, meaning we’re going to go through every item inside our array, and add the value to our variable called “sum”.
So here’s how that looks.
First, we’re gonna create our variable “sum” using an assign tag, and every variable needs at least a placeholder value, so we will set it equal to 0.
{% assign sum = 0 %}
This step is optional, but I realized I named our Custom Attribute a little too wordy, so I’m going to create another variable called “sessions”, and simply set it equal to our Custom Attribute “previous_month_meditation_session_durations_mins”. There’s nothing critical with this step: I’m simply renaming our variable so that our code looks cleaner. Once again, this step is completely optional.
Now, we’re going to set up a For Loop. A For Loop simply goes through every item of an array and executes the same line of code for each item. I like to think of it as a “For Each Loop” and that sometimes helps.
First, here’s the code for the For Loop:
{% for item in sessions %}
INSERT CODE TO EXECUTE
{% endfor %}
Let me explain this in English.
This code says for every item inside our variable sessions, execute this specific code. When we’re done, simply finish the for loop with this endfor statement.
The word “item” is completely arbitrary. We could’ve written for “x”, for “session”, or for “thing” and it all would’ve worked. Developers tend to choose a name that makes the most sense, and “item” generally makes sense, or for our case, I would’ve chosen “session” because every item that we’re going through represent a session.
Whatever variable name we choose, that’s how we will reference the variable in our code to execute.
Okay so let’s start doing some math already!
Sum
So for every “session” in our array called sessions, we are going to set the value of “sum” equal to the existing value of “sum” PLUS the value of “session”. And we will keep adding new values for “session” to our variable “sum” until we are done with the whole list.
And here’s what that looks like:
{% for session in sessions %}
{% assign sum = sum | plus: session %}
{% endfor %}
Behind the scenes, this For Loop has gone through our array of 14 sessions and added all the values to our variable called “sum”.
Let’s see what our “sum” looks like”, and voila, we got 325!
So as my final message, I will simply write:
“Way to go! You’ve meditated for a total of minutes last month!”
And let’s look at our test view one more time, and that looks great!

Thank You!
That’s it for today!
As always, if you have any questions, please share them in the comments. We’re happy to help! 🙏
If you learned something from this video, please subscribe for more awesome Braze videos in the future!
Thank you for watching, and see you next time!
allan@fornowmarketing.com
fornowmarketing.com