This article describes the use cases for Jinja.
Commafy Numbers
Thousands Separator
This will allow you to separate numbers with a comma at the thousandth place. For example:
Input: 1000000
Output: 1,000,000
{% set y = EventAttribute['1'] %}
{{ '{:,}'.format(y)}}
If you want to separate the numbers with a character that is not a comma, use the following code:
{% set y = EventAttribute['1'] %}
{{'{:,}'.format(y) | replace(',','.')}}
Hundreds Separator
This will allow you to separate numbers with a comma at the hundredth place. For example:
Input: 100000
Output: 10,00,00
{% set y = EventAttribute['1'] %}{% set x = y|string %}{% set t = x[-3:] %}{% set h = x[:-3] %}
{% set len = x|length %}{% set r = h|reverse %}
{% set my_list = [] %}
{% for i in range(0,len,2) %}
{{ my_list.append(r[i-2:i])|replace('None','')}}
{% endfor %}
{% set f =my_list|join(',')|reverse + t %}
{% if ',' in f[0] %}{{f[1:]}}{% else %}{{f}}{% endif %}
Generate Dynamic Table
This will allow you to generate a dynamic table in HTML from multiple event attributes.
{% set storeNameList = EventAttribute['StoreName'] %}
{% set storeLinkList = EventAttribute['StoreLink'] %}
{% set storeAddressList = EventAttribute['StoreAddress'] %}
{% for i in storeNameList %}
{% set storeName = storeNameList[loop.index-1] %}
{% set storeLink = storeLinkList[loop.index-1] %}
{% set storeAddress = storeAddressList[loop.index-1] %}
{% endfor %}
| STORE NAME | STORE ADDRESS |
| {{ storeName }} |
{{ storeAddress }}
|
Remove HTML Tags
This will allow you to remove HTML tags from an expression. For example:
Input: <p>Hello!</p>
Output: Hello!
{{<HTML goes here>|striptags}}
Product Set
Generate Comma-Separated Values
This will allow you to generate comma-separated values from a product set. For example:
Input: [{"title":"BTC"},{"title":"DOT"}]
Output: BTC,DOT
{% set y = [{"title":"BTC"},{"title":"DOT"}]|map(attribute='title')|join(',') %}
{{y}}
Occurrence of Values
This will allow you to generate the occurrence of a value from a product set. For example:
Input: [{"title":"BTC"},{"title":"DOT"},{"title":"DOT"}]
Output: 2 (DOT is repeated twice)
{% set y = %}
{{y|selectattr('title', 'equalto', 'DOT')|list|length}}
Highest Occurrence
This will allow you to generate the highest occurrence of a product from a product set. For example:
Input: [{"title":"BTC"},{"title":"DOT"},{"title":"DOT"}]
Output: DOT (DOT is repeated twice)
{% set y = [] %}
{% set new = [] %}
{%for i in y |groupby('title')%}
{{new.append((i.grouper, i.list|length)|list)|replace('None','')}}
{% endfor %}
{% set c = new|sort(attribute=1,reverse=true) %}
{{c[0][0]}}
Calculate Total Price
This will allow you to calculate the sum of a specific attribute (like 'price') for all items in a product set.
Input: [{"price":10},{"price":20},{"price":30}]
Output: 60
{% set y = [{"price":10},{"price":20},{"price":30}] %}
{{ y | map(attribute='price') | sum }}
Recommendation
Display Last Product
This will allow you to print the last product from a product recommendation. If there are five products, the fifth one will be printed.
{% if ProductSet.customValue%}{% for product in ProductSet.customValue[-1:]%}
{{product.title}}
{% endfor %}{% else %}MOE_NOT_SEND{% endif %}
Display All Products - Except Last
This will allow you to print all of the products from a product recommendation except for the last one.
{% if ProductSet.customValue%}{% for product in ProductSet.customValue[:-1]%}
{{product.title}}
{% endfor %}{% else %}MOE_NOT_SEND{% endif %}
Display Random Products
This will allow you to print a defined amount of random products from a recommendation. In the following example, we are printing 5 products.
{% set len = ProductSet.|length %}
{% set n = 5 %}{# Define the number of products to be defined here #}
{% set arr = [] %}
{% for i in range(0,n*3) %}
{% if arr|length <n%} {%="{%" set="set" x="x" len)|random="len)|random" %}="%}" if="if" not="not" in="in" arr="arr" {{="{{" arr.append(x)|replace(="arr.append(x)|replace(" none="None" ,="," )=")" }}="}}" endif="endif" endfor="endfor" for="for" i="i" arr%}{{productset.="arr%}{{ProductSet.">[i].price}}{% endfor %}</n%}>
Print One Product from Each Category
This will allow you to print one product from each category from a recommendation.
{% set newps = [] %}
{% set addedcat = [] %}
{% if ProductSet.Recommendation_recommended_items%}{% for product in ProductSet.Recommendation_recommended_items%}
{% if product.sub_category not in addedcat %}
{{newps.append(product)|replace('None','')}}{{addedcat.append(product.sub_category)|replace('None','')}}
{% endif %}
{% endfor %}{% else %}MOE_NOT_SEND{% endif %}
{% for i in range(0,newps|length) %}
{{newps[i].title}}. {{newps[i].sub_category}}
{% endfor %}
For more information, refer to the Personalize content using Recommendations.
Content API
Fetch and Display Data Conditionally
You can use Jinja to call a Content API to fetch live data and then use that data to personalize your message. In this example, we call a Content API to get property recommendations. The code then checks the data ads_count from the API response. If there is only one property, it displays a singular message. Otherwise, it shows a plural message, dynamically inserting the total count and the minimum price (min_price) from the API response into the text.
{% set conanAPI = ContentApi.pushRecommendationsLive({({"params":{"device_id":"{{UserAttribute['device_id']}}"}})}) %} {% if conanAPI.ads_count == 1 %}See 1 new property from AED {{conanAPI.min_price}}.{% else %}See {{conanAPI.ads_count}} new properties from AED {{conanAPI.min_price}} {% endif %}
Iterate and Display Cart Items
Another common use case is to fetch a user's abandoned cart items and display them in a message. You can retrieve the cart data from a Content API and then use a for loop in Jinja to iterate through each item in the cart and display its details, such as name, price, and image.
For example, your Content API might return a JSON object like this:
{
"date": "March 10, 2016",
"items": [
{
"name": "belt",
"image": "http://mydomain.com/images/1001.jpg",
"price": 11
}, {
"name": "shoes",
"image": "http://mydomain.com/images/1002.jpg",
"price": 12
}, {
"name": "hat",
"image": "http://mydomain.com/images/1003.jpg",
"price": 10
}
]
}
You can then use the following Jinja code to display each item:
{% for cart_item in ContentApi.cart().items %}
<div>
<h2>{{ cart_item.name }}: ${{ cart_item.price|round(2) }}</h2>
<img src="{{ cart_item.image }}" />
</div>
{% endfor %}
For more information, refer to Personalize Content Using Content API.
Auxilliary Data
Fetch and Display User Data
You can use Jinja to fetch additional user information from an auxiliary data source and display it in your message. In this example, the code first gets the user's unique identifier (UserAttribute['uid']). It then uses a custom filter, getAuxData, to look up information in a separate data source named EASY_UI_AUX_DATA. Finally, it accesses the City value from the retrieved data and displays it.
{% set EASY_UI_AUX_DATAData = UserAttribute['uid']|getAuxData('EASY_UI_AUX_DATA') %}
{{EASY_UI_AUX_DATAData.City}}
For more information, refer to Personalize Content Using Auxiliary Data.