Tag

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.

Params:
_(value = “string_toprint”, desc = “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, 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 any elif blocks, finally outputting content of any else block present.

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

Params:
_(value = “items_toiterate”, desc = “Specifies the name of a single item in the sequence or dict.”)

{% for item in items %}
    {{ item }}
{% endfor %}

Ifchanged

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

{% ifchanged variable %}
    Variable to test if changed
{% endifchanged %}

Macro

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

Params:
_(value = “macroname”, desc = “The name given to a macro”)
_(value = “argumentnames”, desc = “Named arguments that are dynamically assigned when the macro is run”)

Basic macro syntax

{% macro name_of_macro(argument_name, argument_name2) %}
    {{ argument_name }}
    {{ argument_name2 }}
{% endmacro %}

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

Echos the result of the expression.

Params:
(value = “expr”, type = “expression”, desc = “Expression to print”)

{% set string_to_echo = "Print me" %}

{% print string_to_echo %}

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.

Params:
(value = “var”, type = “variable identifier”, desc = “The name of the variable”)
(value = “expr”, type = “expression”, desc = “The value stored in the variable (string, number, boolean, or sequence)”)

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.

Params:
(value = “expr”, type = “expression”, desc = “Condition to evaluate”)

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

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.