DEV Community

Rakesh Suryawanshi
Rakesh Suryawanshi

Posted on

4 1

For loop with liquid template in Azure LogicApp

In this blog I will share how to use for loop with liquid templates.

  1. Introduction
  2. When to use?
  3. For loop

Introduction
Liquid is a template language created by Shopify. It's available as an open source project on GitHub, and is used by many different software projects and companies.
more information can be found here.


When to use?
Liquid templates are great tool to bind your UI with XML or json response, additionally you can use these in your Azure Integration services such as logic apps or Azure API management.

recently I have used the liquid template with various scenarios.

documentation to learn liquid templates can be found here.

https://shopify.github.io/liquid/basics/introduction/
Enter fullscreen mode Exit fullscreen mode

For loop
Let's learn how to write loops using Liquid template in various conditions.

Loop all Item of an Array

[
 "apple",
 "mango"
 "banana"
]
Enter fullscreen mode Exit fullscreen mode
"fruits" :[
{% for item in content %}
  {
    "name": "{{item}}"
  }
{% if forloop.last == false %},{% endif %}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop over all items in a collection backwards

[
 "apple",
 "mango"
 "banana"
]
Enter fullscreen mode Exit fullscreen mode

Here we can use reversed keyword to revers the Item of an array.

"fruits" :[
{% for item in content reversed %}
  {
    "name": "{{item}}"
  }
{% if forloop.last == false %},{% endif %}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop over a sorted collection

[
 "apple",
 "mango"
 "banana"
]
Enter fullscreen mode Exit fullscreen mode

Here we can use sort keyword with pipe character (|) to sort the element of an array.

"fruits" :[
{% for item in  (content | sort) %}
  {
    "name": "{{item}}"
  }
{% if forloop.last == false %},{% endif %}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop a certain number of times

{% for i in (0..4) %}
   Iteration {{ i }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop a variable number of times

{$ assign start = 0 %}
{% assign end = 4 %}
{% for i in (start..end) %}
   Iteration {{ i }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

**
Loop a certain number of times backwards**

{% for i in (0..4) reversed %}
   Iteration {{ i }}
{% endfor %}

Enter fullscreen mode Exit fullscreen mode

Loop over an arbitrary range of integers

{% for i in (-3..3) %}
Iteration {{ i }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop over the first 5 items in a collection

[
 "apple",
 "mango"
 "banana",
 "Avocados",
 "Cherry",
 "Blueberries"
]
Enter fullscreen mode Exit fullscreen mode

Here we can use limit keyword to limit the Item of an array.

"fruits" :[
{% for item in content | limit: 5 %}
  {
    "name": "{{item}}"
  }
{% if forloop.last == false %},{% endif %}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Loop over the first 5 items in a collection in reverse order

[
 "apple",
 "mango"
 "banana",
 "Avocados",
 "Cherry",
 "Blueberries"
]
Enter fullscreen mode Exit fullscreen mode

Here we can use reversed keyword to revers the Item of an array.


{% assign rev_content  = content | reverse %}
"fruits" :[
{% for item in rev_content | limit: 5 %}
  {
    "name": "{{item}}"
  }
{% if forloop.last == false %},{% endif %}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

Do something special on the Nth iteration of the loop

[
 "Sunday",
 "Monday"
 "Tuesday",
 "Wednesday",
 "Thursday",
 "Friday",
 "Saturday"
]
Enter fullscreen mode Exit fullscreen mode

Here we can use reversed keyword to revers the Item of an array.


{% for d in content %}
{{ forloop.index }}
{% if forloop.first %}st
{% elsif forloop.index == 2 %}nd
{% elsif forloop.index == 3 %}rd
{% else %}th{% endif %}
 day is {{ d }}
{% endfor %}
Enter fullscreen mode Exit fullscreen mode

That's it I hope you will find this useful.

Thanks!! 🍻🍻🍻🍻.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay