Modify the formatting of your placeholders with filters

In this article, we will explain how to use Brevo Template Language to modify the placeholders in your emails using filters.

🤔 What are filters?

Filters change the output of a placeholder. They are used within double curly braces
{{ }} and separated by a pipe character |, e.g. {{ contact.NAME|upper }}. Multiple filters can be chained, and the output of one filter is applied to the next. 

To add a filter to a placeholder in an email campaign or template, you must add it manually. In the New Drag & Drop Editor, you must also write the placeholder manually, as you cannot add a filter to a placeholder that was added using the Personalization option.

Adding a simple placeholder with the Personalization option
Adding a placeholder with filters manually

đź’¬ Insert a default value

default

Inserts a default fallback value for when the placeholder value is empty.

Input Output
Hello {{ contact.NAME|default:'there' }}, Hello there,

đź”  Format word case

capfirst

Makes the first character of a placeholder value capitalized.

Input Output
{{ contact.MESSAGE|capfirst }} My red car

title

Returns a titlecased version of the placeholder value. Words will start with uppercase letters, all remaining characters are lowercase.

Input Output
{{ contact.NAME|title }} John Doe

upper

Makes each character of a placeholder value uppercase.

Input Output
{{ contact.NAME|upper }} JOHN DOE

lower

Makes each character of a placeholder value lowercase.

Input Output
{{ contact.NAME|lower }} john doe

truncatechars

Shortens a placeholder value if it is longer than the specified number of characters. Truncated values will end with an ellipsis (…) included in the character count.

Input Output
{{ contact.MANAGER|truncatechars:7 }} Elisab…

🔢 Format numbers

floatformat

Rounds the output of your placeholder's float values to the specified decimal place.

To function correctly, the passed values should be passed as a number, rather than as a string, and the values should not be passed in quotes as “value”.

Input Output
{{ contact.BALANCE|floatformat:2 }} 40.32
{{ contact.BALANCE|floatformat:0 }} 40

integer

Converts a number in string or floating point format into an integer number.

Input Output
{{ 10.578|integer }} 10
{{ "16.765"|integer }} 16

float

Converts a number in string or integer format into a floating point number.

Input Output
{{ 10|float }} 10.000000
{{ "16.765"|float }} 16.765000

lang_format

Formats numbers depending on the language of the recipient.

Input Output
{{ params.NUMBER|lang_format:"fr" }} 123 456,789
{{ params.NUMBER|lang_format:"en" }} 123,456.789
{{ params.NUMBER|lang_format:"hi" }} 1,23,456.789
{{ params.NUMBER|lang_format:"bn" }} ১,২৩,৪৫৬.৭৮৯
{{ params.NUMBER|lang_format:"fr,%.2f" }} 123 456,79

Note:

  • To force 2 decimals, add ,%.2f after the language filter.
  • For French, the thousands separator is a non-break space.

đź“… Format date and time

đź’ˇ Good to know
- Day and month abbreviations and full names are supported in English only.
- The reference time is "Monday January 2 15:04:05 -0700 MST 2006".
- If a value is not passed, it’s considered to be 0.
- The default timezone (if not passed) is UTC.
- You may use the am/pm keywords.
- The most common ways to define the timezone are the dedicated keywords (MST, CET, UTC, Asia/Kolkata, etc.), or as numeric, +0100 or -0100, to UTC.

time_parse

Converts your date format (passed as a string) to a standard date format that may be used with other filters.

To pass your current date format as an argument, format the exact date/time of how the reference time would be displayed if it were the value.

Input Output
{{ params.MY_DATE|time_parse:"Monday 02 January 2006" }} Tuesday 11 March 2021

Note: If your string is formatted using RFC3339 you may use the dedicated parser time_parse_rfc3339 without an argument.

date

Converts a date to a specified format.

You will get a date from a string by using the time_parse filter or the current time by passing the filter date to the placeholder time_now.

Input Output
{{ "14:01 01/06/2018"|time_parse:"15:04 02/01/2006"|date:"Mon Jan 2 15:04:05 2006" }} Fri Jun 1 14:01:00 2018
{{ time_now|date:"Monday, Jan 2, 2006" }} Will print the current day and date in the specified format
{{ time_now|date:"02/01/2006" }}  Will print the current day and date in the specified format
{{ time_now|date:"01" }}

 Will print the current month

{{ time_now|date:"02" }}

 Will print the current day

{{ time_now|date:"2006" }}

 Will print the current year

date_i18n

Converts a date to a specified format, but the output will be in other languages.

Currently, the supported languages are en, fr, it, es, de, and pt. This filter is similar to the date filter, and the only difference is the language should be specified in the filter first and then the format.

Input Output
{{ "14:01 01/06/2018" | time_parse:"15:04 02/01/2006"|date_i18n:"en,Monday" }} Friday
{{ "14:01 01/06/2018" | time_parse:"15:04 02/01/2006"|date_i18n:"de,Monday" }} Freitag

time_add_date

Adds number of days to a timestamp or date string. It is mandatory to pass all values to this filter in the following order: "year, month, day".

Input Output
{{ time_now|time_add_date:"1,2,3" }} Will print timestamp after adding 1 year, 2 months, and 3 days to the current timestamp
{{ "14:01 01/06/2018"|time_parse:"15:04 02/01/2006"|time_add_date: "1,2,3" }}
2019-08-04 14:01:00 +0000 UTC

time_in_location

Converts a timestamp or a date string according to the timezone passed to the filter. It is mandatory to pass a timezone to the filter in the following format: "location/timezone”.

Input Output
{{ time_now|time_in_location:"Europe/Berlin" }} Will print current timestamp in CEST
{{ "14:01 01/06/2018"|time_parse:"15:04 02/01/2006"|time_in_location: "Europe/Berlin" }} 2018-06-01 16:01:00 +0200 CEST

time_add_duration

Adds time duration to a timestamp or a date string. It is mandatory to pass at least one value (hour, minute or second) in the filter.

Input Output
{{ time_now|time_add_duration:"1h2m3s" }} Will print timestamp after adding 1 hour, 2 minutes, and 3 seconds to the time value in the current timestamp
{{ time_now|time_add_duration:"30m" }} Will print timestamp after adding 30 minutes to the time value

time_unix

Converts a time value to Unix timestamp in seconds.

Input Output
{{ time_now|time_unix }} Will print current Unix timestamp in seconds

time_unix_nano

Converts a time value to Unix timestamp in nanoseconds.

Input Output
{{ time_now|time_unix_nano }} Will print current Unix timestamp in nanoseconds

🧑‍💻 Encode or encrypt data

base64_encode or decode

Encodes or decodes base 64:

  • base64_decode decodes a string to a []byte
  • base64_encode encodes a []byte to a string
Input Output
https://example.com/test?name={{ contact.FIRSTNAME|base64_encode }} https://example.com/test?name=RW56bw== (instead of https://example.com/test?name=Enzo)

The base64_encode and decode filters can take an optional encoding parameter:

  • std: normal base64 encoding.
  • raw_std: normal base64 encoding, without the = padding characters.
  • url: URL safe version of base64 encoding, + and / are replaced with - and _ respectively.
  • raw_url: a combination of raw and url.

aes_128, 192, or 256

Encrypts a value. For example, you can use this filter to send encrypted contact information in a URL, such as an email address or an ID.

The AES variant used depends on the length of the provided key:

Secret Key Length AES Variant
16 bytes (128 bits) AES-128
24 bytes (192 bits) AES-192
32 bytes (256 bits) AES-256

For example, for the AES-256 variant, the placeholder has to be formatted as {{ contact.ATTRIBUTE | aes_256:"key" }}, where the key:

  • Has to be 32 digits.
  • Has to be randomly generated.
  • Has to be persisted on your end to decrypt the original value.

By default, if no encoding parameter is added, base64 std will be used. Otherwise, it also supports:

  • raw_std: normal base64 encoding, without the = padding characters.
  • url: URL safe version of base64 encoding, + and / are replaced with - and _ respectively.
  • raw_url: a combination of raw and url.
đź’ˇ Good to know

In our examples below, we are encrypting the email addresses of our contacts using the following key: e5a34a481274c776856d04119bed8188. Note that you should not use the same key and instead generate your own key following the above requirements.

Encoding Input Output

No encoding (default base64 encoding std)

{{ contact.EMAIL | aes_256:
"e5a34a481274c776856d04119bed8188" }}
Will print a random string that needs to be decrypted to read the original value

Base64 encoding raw_std

{{ contact.EMAIL | aes_256:
"e5a34a481274c776856d04119bed8188,
raw_std" }}
Will print a random string that needs to be decrypted to read the original value

Base64 encoding url

{{ contact.EMAIL | aes_256:
"e5a34a481274c776856d04119bed8188,
url" }}
Will print a random string that needs to be decrypted to read the original value

Base64 encoding raw_url

{{ contact.EMAIL | aes_256:
"e5a34a481274c776856d04119bed8188,
raw_url" }}
Will print a random string that needs to be decrypted to read the original value

🔎 Other filters

first

Returns the first item of a sequence, a mapping, or a string.

Input API request Output
{{ params.array|first }} "params" :
{
"array" : [ 1,2,3,4 ]
}
1

join

Combines the items in an array into a single string using the argument as a separator.

The separator between elements is empty by default. You may define it with the optional first parameter.

Input Output
{{ [1, 2, 3]|join }} 123
{{ [1, 2, 3]|join: '|' }}
1|2|3

last

Returns the last item of a sequence, a mapping, or a string.

Input Output
{{ [1, 2, 3, 4]|last }} 4

length

Returns the number of items of a sequence or a mapping, or the length of a string.

Input Output
{{ ['a', 'b', 'c', 'd']|length }} 4

safe

Marks a string as not requiring further HTML escaping prior to output.

A

API request

Input Output
"params" :{HtmlTest: "<p>This is my test sentence.</p>"}

{{ params.htmltest|safe }}

This is my test sentence

B

Contact attribute

Placeholder

Input Output
EMAIL_SALUTATION Welcome to <b>Brevo</b>, we're glad to have you here {{ contact.EMAIL_SALUTATION|safe }} Welcome to Brevo, we're glad to have you here

Without the safe filter, the HTML tags would be displayed as a normal string in the recipient’s email: "<p>This is my test sentence</p>" or "Hello from <b>Brevo</b>, nice to have you here".

slice

Extracts a slice of a sequence, a mapping, or a string.

This filter may be configured to:

A Loop through only the first "n" number of elements in an array In both of these cases, remember that elements in an array are assigned a unique position number starting with "0" as the first position (they are "zero-indexed").
B Loop through elements between two specified positions ("n" and "m") in the array
C Loop through elements between two specified elements in an array.  

In each example, consider that your array contains the following: ["a","b","c","d","e","f","g","h"...].

 

Input Output

A

{% for product in params.products|slice:':5' %}
{{ product.name }} - {{ product.price }}
{% endfor %}
Will loop over the first 5 positions (0-4), which are: ["a","b","c","d","e"]
B {% for product in params.products|slice:'2:3' %}
{{ product.name }} - {{ product.price }}
{% endfor %}
Will loop over positions (2-3), which are: ["c","d"]
C {% for product in params.products|slice:'b:d' %}
{{ product.name }} - {{ product.price }}
{% endfor %}
Will loop over the specified elements, which are: ["b","c","d"]

đź“– Additional resources

Brevo Template Language is based on Pongo2, which is a re-implementation of Django in Go (a programming language).

All supported tags

autoescape, comment, cycle, filter, firstof, for, if, ifchanged, now, set, spaceless, templatetag, with.

All supported filters

escape, safe, escapejs, add, addslashes, capfirst, center, cut, date, default, default_if_none, divisibleby, first, floatformat, get_digit, iriencode, join, last, length, length_is, linebreaks, linebreaksbr, linenumbers, ljust, lower, make_list, phone2numeric, pluralize, random, removetags, rjust, slice, stringformat, striptags, time, title, truncatechars, truncatechars_html, truncatewords, truncatewords_html, upper, urlencode, urlize, urlizetrunc, wordcount, wordwrap, yesno.

All supported filters for hash algorithms

hash_md5, hash_sha256, hash_sha256_224, hash_sha512, hash_sha512_224, hash_sha512_256, hash_sha512_384, hash_sha3_224, hash_sha3_256, hash_sha3_384, hash_sha3_512.

đź’ˇ Good to know
If you identify a filter that is not yet supported, please reach out to our customer care team by creating a ticket from your account. If you don't have an account yet, you can contact us here.

⏩ What's next?

🤔 Have a question?

If you have a question, feel free to contact our support team by creating a ticket from your account. If you don't have an account yet, you can contact us here.

If you’re looking for help with a project using Brevo, we can match you with the right certified Brevo expert partner.

đź’¬ Was this article helpful?

9 out of 26 found this helpful