Power Pages Liquid code: forloop.index VS forloop.index0

In a recent Power Pages piece of work I needed to obtation numeric index of the current weekday.

For example, we might want to calculate this week’s Sunday and Saturday dynamically, no matter what day today is. But there is no straight filter for that, and we needed to do some data manipulation to obtain it, as in my previous post.

Previous post approach: Using a case Statement

A simple approach is to use a case statement to map weekday names to numbers:

{% assign today_name = now | date: "dddd" %}
{% assign weekday_num = 0 %}

{% case today_name %}
{% when "Sunday" %}{% assign weekday_num = 0 %}
{% when "Monday" %}{% assign weekday_num = 1 %}
{% when "Tuesday" %}{% assign weekday_num = 2 %}
{% when "Wednesday" %}{% assign weekday_num = 3 %}
{% when "Thursday" %}{% assign weekday_num = 4 %}
{% when "Friday" %}{% assign weekday_num = 5 %}
{% when "Saturday" %}{% assign weekday_num = 6 %}
{% endcase %}

This works well and is clear for anyone reading the code, each weekday is explicitly mapped to its corresponding index.

But there’s also a more dynamic approach we can use.

Alternative Approach: Looping Through an Array

Instead of hardcoding every case, we could instead:

  1. Put all weekdays in a list.
  2. Loop through them.
  3. Detect when we hit today’s name, and use the loop index.

Here’s how it looks:

{% assign today_name = now | date: "dddd" %}

{% assign weekdays = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" | split: "," %}

{% assign weekday_num = 0 %}

{% for day in weekdays %}
  {% if day == today_name %}
    {% assign weekday_num = forloop.index0 %}
  {% endif %}
{% endfor %}

This gives the same result as the case version, but you can reorder the array if your week starts on a different day, and consequently, you would need to shift the values.

Why forloop.index0?

Liquid has two built-in loop counters:

  • forloop.index: starts at 1
  • forloop.index0: starts at 0

Since we want Sunday = 0, Monday = 1, … Saturday = 6, forloop.index0 is the perfect fit.

If you used forloop.index instead, Sunday would be 1, and everything would be shifted by one day.

So, basically, we would need to subtract 1 from the value for a correct result:

{% assign today_name = now | date: "dddd" %}

{% assign weekdays = "Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday" | split: "," %}

{% assign weekday_num = 0 %}

{% for day in weekdays %}
  {% if day == today_name %}
    {% assign weekday_num = forloop.index | minus: 1 %}
  {% endif %}
{% endfor %}

Calculating Sunday and Saturday

Now that we have the weekday number, finding this week’s Sunday and Saturday is easy:

{% assign offsetToSunday = weekday_num | times: -1 %}
{% assign sunday = now | date_add_days: offsetToSunday %}

{% assign saturday = sunday | date_add_days: 6 %}

<script>
  console.log(`This Sunday: {{ sunday | date: "yyyy-MM-dd" }}`);  
  console.log(`This Saturday: {{ saturday | date: "yyyy-MM-dd" }}`);
</script>

So if today is Wednesday, it will move 3 days back to get Sunday, then add 6 days for Saturday.

This approach is useful because it’s easy to customise, for example, you just need to reorder the array if your week starts on Monday. Both the case and loop approaches are valid—this one just gives you more flexibility.

Conclusion

You can use a weekday array with a loop to dynamically find some item’s index in an array, and in the case of weekdays index, forloop.index0 gives you a zero-based index (perfect for Sunday = 0).

This small trick is handy for weekly schedules, custom calendars, or date filters in Power Pages Liquid code.

References

Iteration tags – Microsoft Learn

Leave a Reply

Your email address will not be published. Required fields are marked *