For Loops and Conditionals

Learn how to Use For Loops and Conditionals in Customising Your Template

For Loops

Most of the variables that are passed to the webstore's templates are either single variables with a single value, or they are an iterable. An iterable is a collection of data, normally more than one, that can return each member of the collection one after another.

You can get a full list of all the variables that you can access on the webstore on the Varaibles doc.

If the variable you want to access is iterable, you can iterate over it using a for loop in the template. One of the most commonly seen and widely used iterable is the cart. The cart is a list of dictionaries that shows the quantity and package that are in the cart.

Using the above example, if you wanted to iterate over every item in the cart, display the package's name, price and also the quantity of that package, you could use a for loop, like this:

{% for item in cart %}
<p>{{ item.package.name }} <small>{{ item.package.price }}</small></p>
<p><strong>{{ item.quantity }}</strong></p> {% endfor %}


A conditional is a statement that is evaluated to be True or False. The result of this evaluation is then used to display different things. You can setup a conditional in your template to display one thing when something is True, and something else when another thing is True.

For example, if we wanted to display a message if the cart is empty and a different one when it has items, we could use

{% if cart %}
<p> You have stuff in your cart. </p>
{% else %} <p> You don't have anything in your cart. </p>
{% endif %}

When cart is empty, it's value is None, meaning there is nothing in the cart and it will return the list of items if there is something in the cart. If there is something in the cart, then cart is True, because a list of items is deemed to be True. If there isn't anything in the cart, then the cart is None and it is False meaning the second message will be displayed.

You can also compare values instead of just using True or False, for example

{% if user.username == 'MCxJB' %}
<p> You are MCxJB. </p>
{% else %} <p> You are not MCxJB. </p>
{% endif %}

You can also chain multiple conditions together if you want to do more than one check

{% if user.username == 'MCxJB' %}
<p> You are MCxJB. </p>
{% elif user.username == 'Malachiel' %} <p> You are Malachiel. </p>
{% else %} <p> You are neither MCxJB nor Malachiel. </p>
{% endif %}

The above code checks if the user is MCxJB. If the user is MCxJB, the first piece of text will display. If the user isn't MCxJB, it will check if the user is Malachiel. If the user is Malachiel, it will display the second piece of text. If the user isn't Malachiel, it will then default to the last text. The two first checks are precise checks for value. If neither is True then the last piece of text will be displayed

You can also chain more than one check in an if statement, for example

{% if user.username == 'MCxJB'  and cart %}
<p> You are MCxJB and your cart has items. </p>
{% elif user.username == 'Malachiel' or cart %} <p> You are Malachiel or you have items in your cart. </p>
{% else %} <p> You are neither MCxJB nor Malachiel. </p>
{% endif %}

This one is a little bit more complex. The fist statement will only display the text if the username is MCxJB AND the the user has items in their cart. If this is true the text is displayed, if not, it will move onto the next check. The next one states if the username is Malachiel OR the user has items in their cart. The OR is important as it states that this text will be displayed if the username is Malachiel or the the user has items in their cart.

This means that if the user has items in their cart and their name is not Malachiel, the text will display, if the user has no items in their cart but their name is Malachiel it will display, or if the user has items in their cart and their name is Malachiel it will display.

Have Questions? Open a Support Ticket

View Common Issues on the Knowledgebase

Video Guides on YouTube

Other clients and Donation Store developers hang out on our Discord server, where you can ask for support in #ds-chat, or if you are a Client and you don't uet have your Client role on Discord, let us know and we can add it. Once added you get access to our private Client's support channel.