Logo

dev-resources.site

for different kinds of informations.

For loop with liquid template in Azure LogicApp

Published at
8/4/2022
Categories
logicapps
azure
liquid
Author
rakesh_suryawanshi
Categories
3 categories in total
logicapps
open
azure
open
liquid
open
Author
18 person written this
rakesh_suryawanshi
open
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!! ๐Ÿป๐Ÿป๐Ÿป๐Ÿป.

logicapps Article's
30 articles in total
Favicon
TOP 5 Brain-Boosting Logic Games for Your Phone
Favicon
Security-First Architecture in Azure Logic Apps: Patterns, Practices, and Compliance
Favicon
Serverless Integration: Automate Your Workflow with Logic App
Favicon
Usando PAM no Linux
Favicon
Understanding the Differences between Overriding and Overloading in C#
Favicon
Add Parameters for Workflow Input in Azure Logic Apps
Favicon
AmP Credit Loan Cust0mer CareยฎHelpline Number-8102301568-@7061107023All problem Solhcf.
Favicon
Dynamically Parse JSON as Object or Array
Favicon
Updates to the Azure Onboarding App
Favicon
Automated Onboarding Azure Project
Favicon
Azure Logic Apps: Orchestrating Workflows with Zero Code Complexity" ๐ŸŒ๐Ÿ”ง[5/8]
Favicon
Azure Automated Onboarding Project
Favicon
Real-World Use Cases and Examples of Azure Logic Apps in Action
Favicon
Automate document processing with Form Recognizer and Logic Apps (Part 2)
Favicon
Automate document processing with Form Recognizer and Logic Apps (Part 1)
Favicon
Configuring Azure Logic App Failure Alerts To Stay Ahead
Favicon
Disabling Highly Privileged Azure AD Users with Azure Logic Apps and Microsoft Graph API
Favicon
Power Automate vs Azure Logic Apps
Favicon
How to edit a JSON object inside an Azure Logic App
Favicon
How to solve a DSA problem
Favicon
Using Logic Apps in Power Automate
Favicon
Quickly provision Logic App environment to start developing the flow
Favicon
Why you probably shouldn't use Logic Apps for enterprise integrations
Favicon
f-of-xstate: run some logic on your logic
Favicon
No-code Automation for Azure Boards Using Azure Logic Apps, Power Automate, and Power Virtual Agents
Favicon
Deploy Azure Logic Apps as code
Favicon
For loop with liquid template in Azure LogicApp
Favicon
Liquid template in Logic App
Favicon
Get Logic App Workflow using REST API (Postman)
Favicon
Parameterising Logic App (Standard) connections.json with bicep - Part 3

Featured ones: