In this article, you will find several useful pieces of code that you might commonly use in Bloomreach Engagement, from simple personalization to more complicated ones used as [jinja macros](🔗). You can simply copy-paste them into your own templates.

## Display price with 2 decimal places

If your prices are displaying with only one decimal place, you can use the below Jinja to force 2 decimal places.



## Display customer attribute with a fallback value

Every time you include some customer attribute in the text you need to take into consideration that not all customers have the particular attribute filled. For such cases, you can use a generic fallback value as illustrated below.



## If condition for dynamic content



## Get email domain from email attribute



## Calculate the age of a customer

When customer.date_of_birth is a string in format 2017-01-31, calculated age has 1 year tolerance (does not count with months and days)



Timestamp value in seconds

The value for a timestamp should always be in seconds since there is no automatic recognition of milliseconds based on character number.

## Don't execute an action for a specific customer

You can use the `{% abort %}` command which will stop executing code for a specific customer who meets the given conditions.

For example, using {% abort %} in an email node in a scenario will prevent the email from sending and the scenario will not continue to consequent nodes (for the specific customer). If used in an email, you will be able to see the abort runs in the scenarios test tab (when you hover over the email node).

No event is tracked for `abort` for now. However, if you use the syntax {% abort: "Custom message" %}, the action will not only be aborted, but it will also add a campaign event with status aborted and the Custom message will be in the property message. The custom message can be any Jinja expression, so you are not restricted to only constants. Therefore, you can, for instance, propagate the reason why the action was aborted for a particular customer. Note that while the abort tag is also available in non-actions (conditions, wait nodes, and limits), no event will be added even if the reason is provided.



## Formatting UNIX timestamp after you added or deducted some time



## Formating telephone number

Numbers are often tracked as integers, for instance, 9652508952. You can output them in the telephone number format, such as +7 (965) 250-89-52 using the following macro.



Changing the formating

If you are using different country code, just replace the 7 in the macro by your desired country code. Same for the hyphens, if you prefer to use spaces, just replace the hyphens with spaces.

## Formatting price with custom thousand and decimal separators



## Rounding number down with 1 decimal number precision



## Random dynamic wait time



## Wait time in business days

In this example, the Jinja will set the wait time to be 9 business days, however you can **change this by replacing the number 9 in the part** `{%for counter in range (0,9) %}` **with a desired number of days**.



## Wait 2 days before departure

Please bear in mind that the output values are in seconds.



## Selecting items from the catalog



## Output multiple catalog values for a list of item IDs

This is commonly used in cart abandonment emails when you have a list of items taken from the last cart update of the customer as an aggregate. If you work with JSON and not a simple list, you need to further specify the product when setting the item.



## Get all items from a report



## Get some items from the report and access a catalog

This code takes 2nd-4th item (because the other is present in the report) and looks it up in the catalog based on the first column (item_id). A possible way to show best selling products, etc. without recommendations.



## Multiple IDs for webhooks (for REST API request)



## Lowercase email_id (in REST API request)



If doing JSON API requests, it may actually make more sense to build the request as a dictionary in jinja and serialize the whole thing.



## Selecting the first cookie of a customer



## Convert string from JSON



## Hashing emails with key and time DDYYYY



## Getting gender for SK/CZ



## Delete duplicates from multiple arrays



## Detect if registered identity is not a lowercase email



## Count cookies



## Optimal sending time based on "On Event" trigger



## Parsing item_id from object in cart_update



## Adjusting time from UTC according to winter/summer time



## Output time in the correct timezone



## Hashing a string



## Generate random ulong number



## Removing items from list

To remove an item from a List, use the `list.pop()` method. In case you do not know the index of the item to remove, search for it using `list.index()`. **Example:**



**Prints:** `[10, 30]`

## Get mode (item with the highest occurrence in a list)

  1. Set arguments: list_to_count (required) - List of items whose values should be counted sorting_type (optional) - Whether the resulting list should be ordered ascending or descending or not ordered size (optional) - Amount of items that should be in the returned list

  2. Use the value in `unique_values_count`.



## Accessing information from the catalogs

Instead of ineffectively using, for example, five different calls to the catalog, use just one.



Moreover, you might need to reference the `item_id` in the loop. For example, to check if the item_id is not in the list of excluded items. You can set item_id in the loop when calling the catalog:



Otherwise, using more calls may prolong the email rendering resulting in a [template rendering timeout error](🔗).

## Phone number without symbols, spaces or leading zeros

A Jinja filter to remove symbols, spaces and leading zeros from any phone number format; the output is the phone number in a raw format. _You might need to change the customer attribute name to match yours._