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:

{`

`}

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 }}
{{ contact.sign_up_date \format_date(format='full', locale=‘fr_FR') }}

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 }}
{{ now() \format_datetime(zone='US/Eastern') }}

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 }}

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}}

format_number(locale='en_US')

Return the given decimal number formatted for a specific locale.

Parameters

  • locale – a locale identifier
ValueOutput
{{ 0123456789 \format_number }}

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') }}
{{ 9999.99 \format_currency('EUR', locale=‘FR_fr’) }}

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 }}

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() }}
{{ now('US/Eastern') \format_datetime(format='MMM d YYY') }}

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 }}

add_seconds(n)

Adds n seconds to the date

ValueOutput
{{ contact.sign_up_date \format_datetime }}
{{ contact.sign_up_date \add_seconds(10) \

add_minutes(n)

Adds n minutes to the date

ValueOutput
{{ contact.sign_up_date \format_datetime }}
{{ contact.sign_up_date \add_minutes(10) \

add_hours(n)

Adds n hours to the date

ValueOutput
{{ contact.sign_up_date \format_datetime }}
{{ contact.sign_up_date \add_hours(5) \

add_days(n)

Adds n days to the date

ValueOutput
{{ contact.sign_up_date }}Apr 1, 2016
{{ contact.sign_up_date \add_days(10) }}

add_weeks(n)

Adds n weeks to the date

ValueOutput
{{ contact.sign_up_date }}Apr 1, 2016
{{ contact.sign_up_date \add_weeks(2) }}

add_months(n)

Adds n months to the date

ValueOutput
{{ contact.sign_up_date }}Apr 1, 2016
{{ contact.sign_up_date \add_months(10) }}

add_years(n)

Adds n seconds to the date

ValueOutput
{{ contact.sign_up_date }}Apr 1, 2016
{{ contact.sign_up_date \add_years(2) }}

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 }}

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”) }}

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 }}

escape

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

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

first

Return the first item of a sequence.

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

float

Convert the value into a floating point number.

ValueOutput
{{ 123 \float }}

forceescape

Enforce HTML escaping.

  <th>
    Output
  </th>
</tr>
  <td>
    <li>
  </td>
</tr>
Value
\{\{ “
  • " \| forceescape }}
  • format

    Apply string formatting on an object:

    ValueOutput
    {{ "Hello %s" \format("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" \

    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(" -> ") }}

    last

    Return the last item of a sequence.

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

    length

    Return the number of items of a sequence or mapping.

    ValueOutput
    {{ [1, 2, 3, 4] \length }}

    list

    Convert the value into a list. If it was a string the returned list will be a list of characters.

    ValueOutput
    {{ "hello" \list }}

    lower

    Convert a value to lowercase.

    ValueOutput
    {{ "LOWER" \lower }}

    map

    Applies a filter on a sequence of objects.

    ValueOutput
    {{ ["Hello", "WORLD"] \map("lower") \

    random

    Return a random item from the sequence.

    ValueOutput
    {{ [1, 2, 3] \random }}

    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") \

    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 }}

    string

    Make a string unicode if it isn’t already.

    ValueOutput
    {{ 1 \string }}

    striptags

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

    ValueOutput
    {{ "

    test

    " \
    striptags }}

    subtract_seconds(n)

    Subtract n seconds from the date

    ValueOutput
    {{ contact.sign_up_date \format_datetime }}
    {{ contact.sign_up_date \subtract_seconds(10) \

    subtract_minutes(n)

    Subtract n minutes from the date.

    ValueOutput
    {{ contact.sign_up_date \format_datetime }}
    {{ contact.sign_up_date \subtract_minutes(10) \

    subtract_hours(n)

    Subtract n hours from the date.

    ValueOutput
    {{ contact.sign_up_date \format_datetime }}
    {{ contact.sign_up_date \subtract_hours(5) \

    subtract_days(n)

    Subtract n days from the date.

    ValueOutput
    {{ contact.sign_up_date }}Apr 1, 2016
    {{ contact.sign_up_date \subtract_days(10) }}

    subtract_weeks(n)

    Subtract n weeks from the date.

    ValueOutput
    {{ contact.sign_up_date }}Apr 1, 2016
    {{ contact.sign_up_date \subtract_weeks(2) }}

    subtract_months(n)

    Subtract n months from the date.

    ValueOutput
    {{ contact.sign_up_date }}Apr 1, 2016
    {{ contact.sign_up_date \subtract_months(10) }}

    subtract_years(n)

    Subtract n years from the date.

    ValueOutput
    {{ contact.sign_up_date }}Apr 1, 2016
    {{ contact.sign_up_date \subtract_years(2) }}

    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 }}

    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 }}

    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) }}

    upper

    Convert a value to uppercase.

    ValueOutput
    {{ "lower" \upper }}

    urlencode

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

    ValueOutput
    {{ "myuri.com?cat=123" \urlencode }}

    wordcount

    Count the words in that string.

    ValueOutput
    {{ "a short text" \wordcount }}

    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) }}

    Tests

    defined

    Return true if the variable is defined.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        i is not defined
      </td>
    </tr>
    
    Value
    \{%- if i is defined -%} i is defined \{%- else -%} i is not defined \{%- endif -%}

    divisibleby(num)

    Check if a variable is divisible by a number.

    Parameters

    num – number to check for

      <th>
        Output
      </th>
    </tr>
    
      <td>
        24 is divisible by 3
      </td>
    </tr>
    
    Value
    \{%- if 24 is divisibleby(3) -%} 24 is divisible by 3 \{%- else -%} 24 is not divisible by 3 \{%- endif -%}

    equalto(other)

    Check if an object has the same value as another object.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        3 == 3
      </td>
    </tr>
    
    Value
    \{%- if 3 is equalto(3) -%} 3 == 3 \{%- else -%} 3 != 3 \{%- endif -%}

    even

    Return true if the variable is even.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        3 is not even
      </td>
    </tr>
    
    Value
    \{%- if 3 is even -%} 3 is even \{%- else -%} 3 is not even \{%- endif -%}

    iterate

    Check if it’s possible to iterate over an object.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        you can iterate over an array
      </td>
    </tr>
    
    Value
    \{%- if [1, 2, 3] is iterate -%} you can iterate over an array \{%- else -%} you can't iterate over an array \{%- endif -%}

    lower

    Return true if the variable is lowercased.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        text is not in lowercase
      </td>
    </tr>
    
    Value
    \{%- if "UPPER" is lower -%} text is in lowercase \{%- else -%} text is not in lowercase \{%- endif -%}

    mapping

    Return true if the object is a mapping (dict etc.).

      <th>
        Output
      </th>
    </tr>
    
      <td>
        a dict is a map
      </td>
    </tr>
    
    Value
    \{%- if \{} is mapping -%} a dict is a map \{%- else -%} a dict is not a map \{%- endif -%}

    none

    Return true if the variable is none.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        \{} is not none
      </td>
    </tr>
    
    Value
    \{%- if \{} is none -%} \{} is none \{%- else -%} \{} is not none \{%- endif -%}

    number

    Return true if the variable is a number.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        "3" is not a number
      </td>
    </tr>
    
    Value
    \{%- if "3" is number -%} "3" is a number \{%- else -%} "3" is not a number \{%- endif -%}

    odd

    Return true if the variable is odd.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        3 is odd
      </td>
    </tr>
    
    Value
    \{%- if 3 is odd -%} 3 is odd \{%- else -%} 3 is not odd \{%- endif -%}

    sameas(other)

    Check if an object points to the same memory address than another object.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        \{} and \{} does not have the same memory address
      </td>
    </tr>
    
    Value
    \{%- if \{} is sameas \{} -%} \{} and \{} have the same memory address \{%- else -%} \{} and \{} does not have the same memory address \{%- endif -%}

    sequence

    Return true if the variable is a sequence.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        [1, 4, 8] is a sequence
      </td>
    </tr>
    
    Value
    \{%- if [1, 4, 8] is sequence -%} [1, 4, 8] is a sequence \{%- else -%} [1, 4, 8] is not a sequence \{%- endif -%}

    string

    Return true if the object is a string.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        [1, 4, 8] is not a string
      </td>
    </tr>
    
    Value
    \{%- if [1, 4, 8] is string -%} [1, 4, 8] is a string \{%- else -%} [1, 4, 8] is not a string \{%- endif -%}

    undefined

    Return true if the variable is not defined.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        [1, 4, 8] is not undefined
      </td>
    </tr>
    
    Value
    \{%- if [1, 4, 8] is undefined -%} [1, 4, 8] is undefined \{%- else -%} [1, 4, 8] is not undefined \{%- endif -%}

    upper

    Return true if the variable is uppercased.

      <th>
        Output
      </th>
    </tr>
    
      <td>
        "lowercase text" is not upper
      </td>
    </tr>
    
    Value
    \{%- if "lowercase text" is upper -%} "lowercase text" is upper \{%- else -%} "lowercase text" is not upper \{%- endif -%}

    Global Functions

    range([start, ]stop[, step])

      <th>
        Output
      </th>
    </tr>
    
      <td>
        0 1 2
      </td>
    </tr>
    
    Value
    \{%- for i in range(3) -%} \{\{ i }} \{%- endfor -%}

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

      <th>
        Output
      </th>
    </tr>
    
      <td>
        <p>Enim duis habitasse taciti vivamus hac in, interdum vel eget nisl leo, laoreet pulvinar nam fringilla iaculis, morbi felis. Egestas.</p>
      </td>
    </tr>
    
    Value
    \{\{ lipsum(1) }}

    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.