Jinja
Introduction
Under the hood, the Simon custom context engine is powered by Jinja2 (specifically version 2.10.1). For additional information regarding formatters, filters, tests and global functions, please visit:
https://jinja.palletsprojects.com/en/2.10.x/
You can also view this webinar co-hosted with Scalero about Jinja best practices for some good tips:
Templates vs. content blocks
Using
&
syntax works directly in a template but if you're trying to use it in a content block referenced in a template, then you should use&
instead
Formatters
format_date(format='medium', locale=‘en_US')
Return a date formatted according to the given pattern.
Parameters
- format – Either “full”, “long”, “medium”, or “short”, or a custom date/time pattern
- locale – a locale identifier
Value | Output |
---|---|
{{ contact.sign_up_date | format_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | format_date(format='full', locale=‘fr_FR') }} | vendredi 1 avril 2016 |
format_datetime(format='medium', locale=‘en_US', zone=None)
Return a date (with time) formatted according to the given pattern.
Parameters
- format – Either “full”, “long”, “medium”, or “short”, or a custom date/time pattern
- locale – a locale identifier
- zone - the name of the timezone
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ now() | format_datetime(zone='US/Eastern') }} | Nov 7, 2017, 11:07:56 AM |
format_time(format='medium', locale=‘en_US')
Return a time formatted according to the given pattern.
Parameters
- format – Either “full”, “long”, “medium”, or “short”, or a custom date/time pattern
- locale – a locale identifier
Value | Output |
---|---|
{{ contact.sign_up_date | format_time }} | 6:11:30 PM |
format_decimal(format=None, locale='en_US')
Return the given decimal number formatted for a specific locale.
Parameters
- format
- locale – a locale identifier
Value | Output |
---|---|
{{ 0.123456 | format_decimal}} | 0.123 |
format_number(locale='en_US')
Return the given decimal number formatted for a specific locale.
Parameters
- locale – a locale identifier
Value | Output |
---|---|
{{ 0123456789 | format_number }} | 123,456,789 |
format_currency(locale=‘en_US’, format=None, format_type=‘standard’, currency_digits=None)
Return the given number formatted for a specific locale.
Parameters
- locale – a locale identifier
- format – the format string to use
- format_type – the currency format type to use
- currency_digits – use the currency’s number of decimal digits
Value | Output |
---|---|
{{ 9999.99 | format_currency('USD') }} | $9,999.99 |
{{ 9999.99 | format_currency('EUR', locale=‘FR_fr’) }} | 9 999,99 € |
epoch_to_datetime(timestamp)
Return the UTC datetime corresponding to the POSIX timestamp, without information regarding the timezone.
Parameters
- timestamp – a POSIX timestamp
Value | Output |
---|---|
{{ 1479340535 | epoch_to_datetime }} | 2016-11-16 23:55:35 |
now(zone=None)
Return the current datetime (defaults to UTC if no timezone specified).
Parameters
- zone – a timezone name
Value | Output |
---|---|
{{ now() }} | 2017-11-07 15:45:17.222778 |
{{ now('US/Eastern') }} | 2017-11-07 10:45:17.222796-05:00 |
{{ now('US/Eastern') | format_datetime() }} | Nov 7, 2017, 10:45:17 AM |
{{ now('US/Eastern') | format_datetime(format='MMM d YYY') }} | Nov 7 2017 |
get_timezone(zone=None)
Looks up a timezone by name and returns it.
Parameters
- zone –name of the timezone to look up
Value | Output |
---|---|
{{get_timezone('US/Eastern') }} | US/Eastern |
Filters
abs
Return the absolute value of the argument.
Value | Output |
---|---|
{{ -10 | abs }} | 10 |
add_seconds(n)
Adds n seconds to the date
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | add_seconds(10) | format_datetime }} | Apr 1, 2016, 6:11:40 PM |
add_minutes(n)
Adds n minutes to the date
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | add_minutes(10) | format_datetime }} | Apr 1, 2016, 6:21:30 PM |
add_hours(n)
Adds n hours to the date
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | add_hours(5) | format_datetime }} | Apr 1, 2016, 11:11:40 PM |
add_days(n)
Adds n days to the date
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | add_days(10) }} | Apr 11, 2016 |
add_weeks(n)
Adds n weeks to the date
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | add_weeks(2) }} | Apr 15, 2016 |
add_months(n)
Adds n months to the date
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | add_months(10) }} | Feb 1, 2017 |
add_years(n)
Adds n seconds to the date
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | add_years(2) }} | Apr 1, 2018 |
batch(linecount, fill_with=None)
Returns a list of lists with the given number of items.
Parameters
- linecount – list size
- fill_with – used to fill up missing items
<ol>
{%- for row in [1, 2, 3, 4, 5] | batch(2) %}
<li>
<ul>
{%- for column in row %}
<li>{{ column }}</li>
{%- endfor %}
</ul>
<li>
{%- endfor %}
</ol>
<ol>
<li>
<ul>
<li>1</li>
<li>2</li>
</ul>
</li>
<li>
<ul>
<li>3</li>
<li>4</li>
</ul>
</li>
<li>
<ul>
<li>5</li>
</ul>
<li>
</ol>
capitalize
Capitalize the first letter of the argument.
Value | Output |
---|---|
{{ “hello” | capitalize }} | Hello |
default
If the value is undefined it will return the passed default value, otherwise the value of the variable.
Value | Output |
---|---|
{{ contact.empty_value | default(“default value”) }} | default value |
dictsort(case_sensitive=False, by='key')
Sort a dict and yield (key, value) pairs. You may want to use this function because dicts are unsorted.
Parameters
- case_sensitive – defines if case matters or not
- by – order by either key or value
Value | Output |
---|---|
{{ {'b': 2, 'a': 1} | dictsort }} | [('a', 1), ('b', 2)] |
escape
Convert the characters &, <, >, ‘, and ” in strings to HTML-safe sequences.
Value | Output |
---|---|
{{ “<hel'lo>" | escape }} | <hel'lo> |
first
Return the first item of a sequence.
Value | Output |
---|---|
{{ [1,2,3] | first }} | 1 |
float
Convert the value into a floating point number.
Value | Output |
---|---|
{{ 123 | float }} | 123.0 |
forceescape
Enforce HTML escaping.
Value | Output |
---|---|
{{ “ |
format
Apply string formatting on an object:
Value | Output |
---|---|
{{ "Hello %s" | format("World!") }} | Hello World! |
groupby
Group a sequence of objects by a common attribute.
{%-
set heroes = [
{"name": "batman", "gender": "man"},
{"name": "catwoman", "gender": "female"},
]
-%}
<ul>
{%- for group in heroes | groupby('gender') %}
<li>{{group.grouper}}
<ul>
{%- for person in group.list %}
<li>{{person.name}}</li>
{%- endfor %}
</ul>
</li>
{%- endfor %}
</ul>
<ul>
<li>
female
<ul>
<li>catwoman</li>
</ul>
</li>
<li>
man
<ul>
<li>batman</li>
</ul>
</li>
</ul>
int(default=0, base=10)
Convert the value into an integer.
Parameters
- default – value returned if the conversion does not work
- base – override the default base (10)
Value | Output |
---|---|
{{ “10" | int + “10" | int }} | 20 |
join(d=u'', attribute=None)
Return a string which is the concatenation of the strings in the sequence.
Parameters
- d – separator (empty string by default)
- attribute – if the value is an object, define the name of the attributes to join together
Value | Output |
---|---|
{{ [1, 2, 3, 4] | join(" -> ") }} | 1 -> 2 -> 3 -> 4 |
last
Return the last item of a sequence.
Value | Output |
---|---|
{{ ["first", "last"] | last }} | last |
length
Return the number of items of a sequence or mapping.
Value | Output |
---|---|
{{ [1, 2, 3, 4] | length }} | 4 |
list
Convert the value into a list. If it was a string the returned list will be a list of characters.
Value | Output |
---|---|
{{ "hello" | list }} | ['h', 'e', 'l', 'l', 'o'] |
lower
Convert a value to lowercase.
Value | Output |
---|---|
{{ "LOWER" | lower }} | lower |
map
Applies a filter on a sequence of objects.
Value | Output |
---|---|
{{ ["Hello", "WORLD"] | map("lower") | join(", ") }} | hello, world |
random
Return a random item from the sequence.
Value | Output |
---|---|
{{ [1, 2, 3] | random }} | 2 |
reject
Filters a sequence of objects by applying a test to each object, and rejecting the objects with the test succeeding. If no test is specified, each object will be evaluated as a boolean.
Value | Output |
---|---|
{{ [1, 2, 3, 4] | reject("odd") | join(", “) }} | 2, 4 |
slice(slices, fill_with=None)
Slice an iterator and return a list of lists containing those items.
Parameters
- slices – number of slices
- fill_with – used to fill missing values on the last iteration
<ol>
{%- for column in [1, 2, 3, 4, 5] | slice(2) %}
<li>
<ul>
{%- for item in column %}
<li>{{ item }}</li>
{%- endfor %}
</ul>
</li>
{%- endfor %}
</ol>
<ol>
<li>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
</li>
<li>
<ul>
<li>4</li>
<li>5</li>
</ul>
</li>
</ol>
sort(reverse=False, case_sensitive=False, attribute=None):
Sort an iterable.
Parameters
- reverse – sort in descending order
- case_sensitive – controls case sensitiveness
- attribute – if the value is an object, define the name of the attributes to sort from
Value | Output |
---|---|
{{ [3, 2, 5, 1, 4] | sort }} | [1, 2, 3, 4, 5] |
string
Make a string unicode if it isn’t already.
Value | Output |
---|---|
{{ 1 | string }} | 1 |
striptags
Strip SGML/XML tags and replace adjacent whitespace by one space.
Value | Output |
---|---|
{{ " test " | striptags }} | test |
subtract_seconds(n)
Subtract n seconds from the date
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | subtract_seconds(10) | format_datetime }} | Apr 1, 2016, 6:11:20 PM |
subtract_minutes(n)
Subtract n minutes from the date.
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | subtract_minutes(10) | format_datetime }} | Apr 1, 2016, 6:01:30 PM |
subtract_hours(n)
Subtract n hours from the date.
Value | Output |
---|---|
{{ contact.sign_up_date | format_datetime }} | Apr 1, 2016, 6:11:30 PM |
{{ contact.sign_up_date | subtract_hours(5) | format_datetime }} | Apr 1, 2016, 1:11:40 PM |
subtract_days(n)
Subtract n days from the date.
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | subtract_days(10) }} | Mar 22, 2016 |
subtract_weeks(n)
Subtract n weeks from the date.
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | subtract_weeks(2) }} | Mar 18, 2016 |
subtract_months(n)
Subtract n months from the date.
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | subtract_months(10) }} | Jun 1, 2015 |
subtract_years(n)
Subtract n years from the date.
Value | Output |
---|---|
{{ contact.sign_up_date }} | Apr 1, 2016 |
{{ contact.sign_up_date | subtract_years(2) }} | Apr 1, 2014 |
sum(attribute=None, start=0)
Returns the sum of a sequence of numbers plus the value of parameter ‘start’ (which defaults to 0). When the sequence is empty it returns start.
Parameters
- attribute – if the value is an object, define the name of the attributes to sum together
- start – sum this number with the rest of the sequence
Value | Output |
---|---|
{{ [1, 2, 3] | sum }} | 6 |
title
Return a titlecased version of the value. I.e. words will start with uppercase letters, all remaining characters are lowercase.
Value | Output |
---|---|
{{ "this is a title" | title }} | This Is A Title |
truncate(length=255, killwords=False, end='...')
Return a truncated copy of the string.
Parameters
length – max length of the text
killwords – If true the filter will cut the text at length, otherwise it will discard the last word
end – customize the ellipsis text
Value | Output |
---|---|
{{ "a long text" | truncate(9, True) }} | a long... |
upper
Convert a value to uppercase.
Value | Output |
---|---|
{{ "lower" | upper }} | LOWER |
urlencode
Escape strings for use in URLs (uses UTF-8 encoding).
Value | Output |
---|---|
{{ "myuri.com?cat=123" | urlencode }} | myuri.com%3Fcat%3D123 |
wordcount
Count the words in that string.
Value | Output |
---|---|
{{ "a short text" | wordcount }} | 3 |
wordwrap(width=79, break_long_words=True)
Return a copy of the string passed to the filter wrapped after 79 characters.
Parameters
width – number of characters to wrap after
break_long_words – if false, will not split words apart if they are longer than width
Value | Output |
---|---|
{{ "a long text" | wordwrap(2) }} | a lo ng te xt |
Tests
defined
Return true if the variable is defined.
Value | Output |
---|---|
{%- if i is defined -%} i is defined {%- else -%} i is not defined {%- endif -%} | i is not defined |
divisibleby(num)
Check if a variable is divisible by a number.
Parameters
num – number to check for
Value | Output |
---|---|
{%- if 24 is divisibleby(3) -%} 24 is divisible by 3 {%- else -%} 24 is not divisible by 3 {%- endif -%} | 24 is divisible by 3 |
equalto(other)
Check if an object has the same value as another object.
Value | Output |
---|---|
{%- if 3 is equalto(3) -%} 3 == 3 {%- else -%} 3 != 3 {%- endif -%} | 3 == 3 |
even
Return true if the variable is even.
Value | Output |
---|---|
{%- if 3 is even -%} 3 is even {%- else -%} 3 is not even {%- endif -%} | 3 is not even |
iterate
Check if it’s possible to iterate over an object.
Value | Output |
---|---|
{%- if [1, 2, 3] is iterate -%} you can iterate over an array {%- else -%} you can't iterate over an array {%- endif -%} | you can iterate over an array |
lower
Return true if the variable is lowercased.
Value | Output |
---|---|
{%- if "UPPER" is lower -%} text is in lowercase {%- else -%} text is not in lowercase {%- endif -%} | text is not in lowercase |
mapping
Return true if the object is a mapping (dict etc.).
Value | Output |
---|---|
{%- if {} is mapping -%} a dict is a map {%- else -%} a dict is not a map {%- endif -%} | a dict is a map |
none
Return true if the variable is none.
Value | Output |
---|---|
{%- if {} is none -%} {} is none {%- else -%} {} is not none {%- endif -%} | {} is not none |
number
Return true if the variable is a number.
Value | Output |
---|---|
{%- if "3" is number -%} "3" is a number {%- else -%} "3" is not a number {%- endif -%} | "3" is not a number |
odd
Return true if the variable is odd.
Value | Output |
---|---|
{%- if 3 is odd -%} 3 is odd {%- else -%} 3 is not odd {%- endif -%} | 3 is odd |
sameas(other)
Check if an object points to the same memory address than another object.
Value | Output |
---|---|
{%- if {} is sameas {} -%} {} and {} have the same memory address {%- else -%} {} and {} does not have the same memory address {%- endif -%} | {} and {} does not have the same memory address |
sequence
Return true if the variable is a sequence.
Value | Output |
---|---|
{%- if [1, 4, 8] is sequence -%} [1, 4, 8] is a sequence {%- else -%} [1, 4, 8] is not a sequence {%- endif -%} | [1, 4, 8] is a sequence |
string
Return true if the object is a string.
Value | Output |
---|---|
{%- if [1, 4, 8] is string -%} [1, 4, 8] is a string {%- else -%} [1, 4, 8] is not a string {%- endif -%} | [1, 4, 8] is not a string |
undefined
Return true if the variable is not defined.
Value | Output |
---|---|
{%- if [1, 4, 8] is undefined -%} [1, 4, 8] is undefined {%- else -%} [1, 4, 8] is not undefined {%- endif -%} | [1, 4, 8] is not undefined |
upper
Return true if the variable is uppercased.
Value | Output |
---|---|
{%- if "lowercase text" is upper -%} "lowercase text" is upper {%- else -%} "lowercase text" is not upper {%- endif -%} | "lowercase text" is not upper |
Global Functions
range([start, ]stop[, step])
Value | Output |
---|---|
{%- for i in range(3) -%} {{ i }} {%- endfor -%} | 0 1 2 |
lipsum(n=5, html=True, min=20, max=100)
Value | Output |
---|---|
{{ lipsum(1) }} | Enim duis habitasse taciti vivamus hac in, interdum vel eget nisl leo, laoreet pulvinar nam fringilla iaculis, morbi felis. Egestas. |
dict(**items)
Value | Output |
---|---|
{{ dict(foo='bar') }} | {'foo': 'bar'} |
Other Variables
Simon makes other variables available via Custom Context to be able to be displayed in campaigns or passed as metadata for tracking purposes. These variables are not contact properties; they are created when a campaign happens.
Value | Output | Description |
---|---|---|
{{simon.operation_id}} | 00000000-0000-0000-0000-000000000000 | A unique identifier for each message sent to a contact or sync to a channel. The value is in UUID format . |
Operation ID in Preview or Test Send
When using the
operation_id
variable in either the Custom Context preview window or a test send, a dummy value is used. Since this value is generated for each operation of a flow, it does not exist for a flow in draft mode. The value will be displayed as00000000-0000-0000-0000-000000000000
.
Updated 7 months ago