Modify the output of a variable using filters

Variables, also known as personalization tags or merge tags, let you insert personalized data for each recipient, like their first name or most recent purchase. Filters allow you to control how this data appears by adjusting its format or content.

What are filters?

Filters modify the output of a variable. They are used inside double curly braces {{ }} and are separated from the variable by a pipe character |. For example, {{ contact.FIRSTNAME|upper }} will display the contact’s name in uppercase.

You can also chain multiple filters, meaning the output of one filter is passed to the next.

To use a filter in an email campaign or template, you need to manually add it to the variable. This means writing the entire variable yourself.

In the Drag & Drop editor, the variable will then appear as a tag, without the filter.

email_filters_example.gif
💡 Good to know
You can't apply a filter to a variable added through the Add variable menu. To use filters, type the variable manually instead.

💬 Insert a default value

default

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

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

🔠 Format word case

capfirst

Makes the first character of a variable value capitalized.

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

title

Returns a titlecased version of a variable value. Words will start with uppercase letters; all remaining characters will be lowercase.

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

upper

Makes each character of a variable value uppercase.

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

lower

Makes each character of a variable value lowercase.

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

truncatechars

Shortens a variable 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 variable'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 they should not be passed in quotes as "value".

Input Output
{{ params.total|floatformat:2 }} 40.32
{{ params.total|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
💡 Good to know
  • To force 2 decimals, add ,%.2f after the language filter.
  • For French, the thousands separator is a non-break space.

📅 Format date and time

❗️ Important
  • 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
💡 Good to know

If your string is formatted using RFC3339, you can 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 variable 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 a 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 variable 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"]

verbatim

In Brevo, variables are written using double curly brackets like {{ params.property }}. When you include double curly brackets like {{ this }} in your email, Brevo treats it as a template element.

To display {{ }} as plain text in your email, wrap the content in a {% verbatim %} block:

Input Output
{% verbatim %}
{{ Print variable }}
{% endverbatim %}
{{ Print variable }}

autoescape

By default, Brevo escapes all variable content, including HTML and JavaScript. This means that if your variable contains HTML (e.g., <h1>My title</h1>), it will be displayed as plain text rather than rendered as HTML.

To allow HTML rendering, you need to disable auto-escaping using the {% autoescape off %} tag. Wrap the variable in this block and close it with {% endautoescape %}:

{% autoescape off %}{{ params.my_html }}{% endautoescape %}

  Input Output
Without the autoescape tag {{ params.my_html }} <h1>My title</h1>
With the autoescape tag

{% autoescape off %}
{{ params.my_html }}
{% endautoescape %}

My title

📖 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, contact our support team by creating a ticket from your account. If you don't have an account yet, you can contact us here.

🤔 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 Agency partner.

💬 Was this article helpful?

15 out of 43 found this helpful