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 &amp 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{ 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
ValueOutput
{{get_timezone('US/Eastern') }}US/Eastern

Filters

abs

Return the absolute value of the argument.

ValueOutput
{{ -10 | abs }}10

add_seconds(n)

Adds n seconds to the date

ValueOutput
{{ 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

ValueOutput
{{ 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

ValueOutput
{{ 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

ValueOutput
{{ 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

ValueOutput
{{ 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

ValueOutput
{{ 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

ValueOutput
{{ 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.

ValueOutput
{{ “hello” | capitalize }}Hello

default

If the value is undefined it will return the passed default value, otherwise the value of the variable.

ValueOutput
{{ 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
ValueOutput
{{ {'b': 2, 'a': 1} | dictsort }}[('a', 1), ('b', 2)]

escape

Convert the characters &, <, >, ‘, and ” in strings to HTML-safe sequences.

ValueOutput
{{ “<hel'lo>" | escape }}<hel'lo>

first

Return the first item of a sequence.

ValueOutput
{{ [1,2,3] | first }}1

float

Convert the value into a floating point number.

ValueOutput
{{ 123 | float }}123.0

forceescape

Enforce HTML escaping.

ValueOutput
{{ “
  • " | forceescape }}
  • format

    Apply string formatting on an object:

    ValueOutput
    {{ "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)
    ValueOutput
    {{ “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
    ValueOutput
    {{ [1, 2, 3, 4] | join(" -> ") }}1 -> 2 -> 3 -> 4

    last

    Return the last item of a sequence.

    ValueOutput
    {{ ["first", "last"] | last }}last

    length

    Return the number of items of a sequence or mapping.

    ValueOutput
    {{ [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.

    ValueOutput
    {{ "hello" | list }}['h', 'e', 'l', 'l', 'o']

    lower

    Convert a value to lowercase.

    ValueOutput
    {{ "LOWER" | lower }}lower

    map

    Applies a filter on a sequence of objects.

    ValueOutput
    {{ ["Hello", "WORLD"] | map("lower") | join(", ") }}hello, world

    random

    Return a random item from the sequence.

    ValueOutput
    {{ [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.

    ValueOutput
    {{ [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
    ValueOutput
    {{ [3, 2, 5, 1, 4] | sort }}[1, 2, 3, 4, 5]

    string

    Make a string unicode if it isn’t already.

    ValueOutput
    {{ 1 | string }}1

    striptags

    Strip SGML/XML tags and replace adjacent whitespace by one space.

    ValueOutput
    {{ "

    test

    " | striptags }}
    test

    subtract_seconds(n)

    Subtract n seconds from the date

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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.

    ValueOutput
    {{ 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
    ValueOutput
    {{ [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.

    ValueOutput
    {{ "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

    ValueOutput
    {{ "a long text" | truncate(9, True) }}a long...

    upper

    Convert a value to uppercase.

    ValueOutput
    {{ "lower" | upper }}LOWER

    urlencode

    Escape strings for use in URLs (uses UTF-8 encoding).

    ValueOutput
    {{ "myuri.com?cat=123" | urlencode }}myuri.com%3Fcat%3D123

    wordcount

    Count the words in that string.

    ValueOutput
    {{ "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

    ValueOutput
    {{ "a long text" | wordwrap(2) }}a lo ng te xt

    Tests

    defined

    Return true if the variable is defined.

    ValueOutput
    {%- 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

    ValueOutput
    {%- 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.

    ValueOutput
    {%- if 3 is equalto(3) -%}
    3 == 3
    {%- else -%}
    3 != 3
    {%- endif -%}
    3 == 3

    even

    Return true if the variable is even.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.).

    ValueOutput
    {%- 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.

    ValueOutput
    {%- if {} is none -%}
    {} is none
    {%- else -%}
    {} is not none
    {%- endif -%}
    {} is not none

    number

    Return true if the variable is a number.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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.

    ValueOutput
    {%- 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])

    ValueOutput
    {%- for i in range(3) -%}
    {{ i }}
    {%- endfor -%}
    0 1 2

    lipsum(n=5, html=True, min=20, max=100)

    ValueOutput
    {{ lipsum(1) }}

    Enim duis habitasse taciti vivamus hac in, interdum vel eget nisl leo, laoreet pulvinar nam fringilla iaculis, morbi felis. Egestas.

    dict(**items)

    ValueOutput
    {{ 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 or lookup variables; they are created when a campaign happens.

    ValueOutputDescription
    {{simon.operation_id}}00000000-0000-0000-0000-000000000000A 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 as 00000000-0000-0000-0000-000000000000.