Jinjava tags

Tags are jinjava-based functions. They enable easier access to some jinjava expressions, with additional or alternative logic tailored to the needs of our clients.

Tag delimiters

They can use two types of delimiters:

  • {% %} - available in all tags.
  • {%- -%} - available in tags that have an opening and closing statement. Unnecessary whitespace inside the tags is removed. Using these delimiters where possible is recommended for improved performance.

Example:

Input:

{% set b = 'I remove the whitespace' %}

{%- if b -%}

{{b}}

End of IF
{%- endif -%}

Data sent to the browser:

"I remove the whitespace\nEnd of IF"

Append

You can use do append to add an item to a list.

Syntax:

{% do listname.append('string')}

Example:
This example uses do.append and loop.index to iterate over a list of items and pull corresponding values from another list to create an array of objects with values from both lists.

<!-- List of items: -->
{% set itemsArray = ['item1','item2','item3'] %}
<!-- List of prices for the items: -->
{% set pricesArray = ['item1_price','item2_price','item3_price'] %}
<!-- Empty array to fill with objects: -->
{% set arrayWithItemsAndPrices = [] %}

<!-- Start iterating over itemsArray -->
{%- for item in itemsArray -%}
    <!-- Set variable to store index of the current iteration: -->
    {% set index=loop.index0 %}
    <!-- Append object to the array -->
    {% do arrayWithItemsAndPrices.append({
        item: item,
        price: pricesArray[index], 
        index: index 
        })
    %}
{%- endfor -%}

In the object:

  • item is the current item from itemsArray,
  • price is pulled from the corresponding index from pricesArray,
  • index is the current iteration

Result:
The arrayWithItemsAndPrices array is the following:

[
    {item=item1, price=item1_price, index=0},
    {item=item2, price=item2_price, index=1},
    {item=item3, price=item3_price, index=2}
]

Tip: The items and prices could be inserted from an aggregate.

AutoEscape

Autoescape the tag’s contents.

{%- autoescape -%}
    <!--Code to escape-->
{%- endautoescape -%}

Call

In some cases, it can be useful to pass a macro to another macro. For this purpose, you can use the special call block.

This is a simple dialog rendered by using a macro and a call block:

{%- macro render_dialog(title, class='dialog') -%}
    <div class="{{ class }}">
        <h2>{{ title }}</h2>
        <div class="contents">
            {{ caller() }}
        </div>
    </div>
{%- endmacro -%}

{%- call render_dialog('Hello World') -%}
    This is a simple dialog rendered by using a macro and
    a call block.
{%- endcall -%}

It’s also possible to pass arguments back to the call block. This makes it useful as a replacement for loops. Generally speaking, a call block works exactly like a macro, but it doesn’t have a name. Here’s an example of how a call block can be used with arguments:

{%- macro dump_users(users) -%}
    <ul>
    {%- for user in users -%}
        <li>
            <p>{{ user.username|e }}</p>
            {{ caller(user) }}
        </li>
    {%- endfor -%}
    </ul>
{%- endmacro -%}

{%- call(user) dump_users(list_of_user) -%}
    <dl>
        <dl>Realname</dl>
        <dd>{{ user.realname|e }}</dd>
        <dl>Description</dl>
        <dd>{{ user.description }}</dd>
    </dl>
{%- endcall -%}

Cycle

The cycle tag can be used within a for loop to cycle through a series of string values and print them with each iteration.

Parameters:

Type Required Description
list yes A comma-separated list of strings to print with each iteration. The list will repeat if there are more iterations than string parameter values.

In the example below, the classes odd and even are applied to posts in a listing:

{%- for content in contents -%}
    <div class="post-item {% cycle 'odd','even' %}">
        Blog post content
    </div>
{%- endfor -%}

If, else if, else

Outputs inner content if expression evaluates to true, otherwise evaluates elif blocks, finally outputting the content of the else block present (if no elif block evaluated to true).

{%- if number <= 2 -%}
    Variable named number is less than or equal to 2.
{%- elif number <= 4 -%}
    Variable named number is less than or equal to 4.
{%- elif number <= 6 -%}
    Variable named number is less than or equal to 6.
{%- else -%}
    Variable named number is greater than 6.
{%- endif -%}

For

Outputs the inner content for each item in the given iterable.

Example:

{% set names = ["John", "Kate", "Bob"] %}

{%- for item in names -%}
    Hello, {{ item }}!
{%- endfor -%}

Loop variables

Inside the for loop, you can access special variables.

col1 col2
loop.index The current iteration, first iteration is 1
loop.index0 The current iteration, first iteration is 0
loop.revindex Iterations until end of loop, last iteration is 1
loop.revindex0 Iterations until end of loop, last iteration is 0
loop.first true if this is the first iteration
loop.last true if this is the last iteration
loop.length Total number of items in the loop
loop.cycle() Helper function for cycling, see example below the table
loop.depth The depth of the current iteration in a recursive loop, starting at 1
loop.depth0 The depth of the current iteration in a recursive loop, starting at 0

Example

Input:

{% set array = ["q","w","e","r","t"] %}
{%- for item in array -%}
Item: {{ item }}
Index: {{ loop.index }}
Revindex: {{ loop.revindex }}
Cycle: {{ loop.cycle('foo','bar','baz') }}
{%- endfor -%}

Output:

Item: q
Index: 1
Revindex: 5
Cycle: foo

Item: w
Index: 2
Revindex: 4
Cycle: bar

Item: e
Index: 3
Revindex: 3
Cycle: baz

Item: r
Index: 4
Revindex: 2
Cycle: foo

Item: t
Index: 5
Revindex: 1
Cycle: bar

Get

See “Object properties” in “Insert usage”.

Ifchanged

Outputs the tag contents if the given variable has changed since a prior invocation of this tag.

{%- ifchanged variable -%}
    <!-- Code to execute if the variable has changed -->
{%- endifchanged -%}

Macro

Macros allow you to print multiple statements with a dynamic value or values.

Basic macro syntax:

<!-- Defining the macro -->
{%- macro name_of_macro(argument_name, argument_name2) -%}
    {{ argument_name }}
    {{ argument_name2 }}
{%- endmacro -%}

<!-- Calling the macro -->
{{ name_of_macro("value to pass to argument 1", "value to pass to argument 2") }}

Example of a macro used to print CSS3 properties with the various vendor prefixes.

{%- macro trans(value) -%}
    -webkit-transition: {{value}};
    -moz-transition: {{value}};
    -o-transition: {{value}};
    -ms-transition: {{value}};
    transition: {{value}};
{%- endmacro -%}

The macro can then be called like a function. The macro is printed for anchor tags in CSS.

a { {{ trans("all .2s ease-in-out") }} }

Print

Echoes the result of the expression.

Examples:

{% set string_to_echo = "Print me" %}

{% print string_to_echo %}

{% print -65|abs %}

Range

Generates an array of integers. The array can’t be longer than 1000 items.

range(start,stop,step)
Parameter Required Default Description
start no 0 The first value in the array.
stop yes n/a The limit at which the array ends. The value of the limit is excluded from the array.
step no 1 The increment between items. Can be negative.

Examples:

Default start and step:

{% set foo=range(5) %}
{{ foo }}

OUTPUT:
[0, 1, 2, 3, 4]

Start defined, step default:

{% set foo=range(2,10) %}
{{foo}}

OUTPUT:
[2, 3, 4, 5, 6, 7, 8, 9]

Defined start and step. The value of stop isn’t included in the array.

{% set foo=range(2,10,2) %}
{{foo}}

OUTPUT:
[2, 4, 6, 8]

Negative increment. The value of stop isn’t included in the array. In this case, start must be higher than stop!

{% set foo=range(10,2,-2) %}
{{foo}}

OUTPUT:
[10, 8, 6, 4]

Raw

Processes all inner expressions as plain text.

{%- raw -%}
    The personalization token for a contact's first name is {{ contact.firstname }}
{%- endraw -%}

Set

Assigns the value or result of a statement to a variable.

Basic syntax:

{% set variableName = variableValue %}

The value can be a string, a number, a boolean, or a sequence.

Example:

Set a variable and print the variable in an expression:

{% set primaryColor = "#F7761F" %}

{{ primaryColor }}

You can combine multiple values or variables into a sequence variable.

{% set var_one = "String 1" %}

{% set var_two = "String 2" %}

{% set sequence = [var_one, var_two] %}

Unless

Unless is a conditional just like ‘if’, but works on an inverse logic.

{%- unless x < 0 -%}
    x is greater than zero
{%- endunless -%}

Update

Creates or updates the properties of an object.

Example:

{% set product = {'category':'sneakers'} %}

Initial object: {{product}}
<br>
{% set colorData = {'color':'red','size': 8} %}
{% do product.update(colorData) %}
Added color and size: {{product}}
<br>
{% set newColorData = {'color':'blue'} %}
{% do product.update(newColorData) %}
Updated color: {{product}}

Output:

Initial object: {category=sneakers}
<br>
Added color and size: {category=sneakers, color=red, size=8}
<br>
Updated color: {category=sneakers, color=blue, size=8}

😕

We are sorry to hear that

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

😉

Awesome!

Thank you for helping improve out documentation. If you need help or have any questions, please consider contacting support.

Close modal icon Placeholder alt for modal to satisfy link checker