Templates Syntax

Inside a component you write HTML, and three kinds of marks:

Mark What it is for
{{ ... }} Render a value
{% ... %} Do something: a condition, a loop, a variable
{# ... #} A comment. It is not rendered

If you have used Jinja, this will look familiar: Jx uses the same syntax, with some differences.

Rendering Values

{{ }} renders the value of an expression:

{#def user, items #}

<p>Hello, {{ user.name }}!</p>
<p>You have {{ items | len }} items.</p>

The values are escaped, so text that comes from your users can't add HTML to your pages. See Escaping.

Expressions

Expressions are written as in Python:

{{ user.name }}             {# An attribute #}
{{ user["name"] }}          {# An item #}
{{ name.upper() }}          {# A method #}
{{ price * 1.2 }}           {# Math: + - * / // % ** #}
{{ "Hi, " ~ name }}         {# Join as text #}
{{ "yes" if active else "no" }}
{{ count > 0 and not hidden }}
{{ role in ["admin", "editor"] }}

. works for attributes and for the items of a dictionary: user.name is the same as user["name"] when user is a dictionary.

true, false and none can also be written as in Python: True, False and None.

Names

A name in an expression is one of these:

  • An argument of the component, declared in its {#def #}.
  • A variable made with {% set %} or by a {% for %}.
  • A global of the catalog.

Using any other name is an error:

{{ titel }}
KeyError: 'titel'
`titel` is not defined: it is not an argument of card.jx, a variable set in it, nor a global of the catalog

To use a name that may not exist, give it a default value or ask first:

{{ subtitle | default("No subtitle") }}

{% if is_defined(subtitle) %}
  <p>{{ subtitle }}</p>
{% endif %}

Note

The functions of Python, like len() or str(), are not available as names. Use a filter (items | len, value | str) or add the function to the globals of the catalog. The exceptions are range() and pow(), that you can always use.

Filters

A filter changes a value. It is written after the value, with a |:

{{ title | upper }}
{{ names | join(", ") }}
{{ description | truncate(80) | capitalize }}

Jx comes with many filters, and you can add your own.

Conditions

{% if user.is_admin %}
  <a href="/admin">Admin</a>
{% elif user.is_editor %}
  <a href="/drafts">Drafts</a>
{% else %}
  <a href="/profile">Profile</a>
{% endif %}

Loops

<ul>
{% for item in items %}
  <li>{{ item.name }}</li>
{% else %}
  <li>Nothing here yet.</li>
{% endfor %}
</ul>

The {% else %} part is optional. It is rendered when there are no items.

To get the keys and the values of a dictionary:

{% for name, value in settings.items() %}
  <dt>{{ name }}</dt>
  <dd>{{ value }}</dd>
{% endfor %}

To skip some items, add a condition:

{% for user in users if user.active %}
  <li>{{ user.name }}</li>
{% endfor %}

The loop Variable

Inside a loop, loop tells you where you are:

loop.index The number of the current item, starting at 1
loop.index0 The same, starting at 0
loop.first true for the first item
loop.last true for the last item
loop.length How many items there are
loop.revindex How many items are left, counting this one
loop.previtem The item before, or none
loop.nextitem The item after, or none
loop.cycle("a", "b") One of its arguments, taking turns
{% for name in names %}
  <li class="{{ loop.cycle('odd', 'even') }}">
    {{ loop.index }} of {{ loop.length }}: {{ name }}
  </li>
{% endfor %}

Variables

{% set %} makes a variable:

{% set full_name = user.first_name ~ " " ~ user.last_name %}

<h1>{{ full_name }}</h1>

A {% set %} inside a loop can change a variable made before it:

{% set total = 0 %}
{% for item in items %}
  {% set total = total + item.price %}
{% endfor %}

<p>Total: {{ total }}</p>

Other Statements

do

Runs an expression without rendering its result:

{% do attrs.set(role="alert") %}

raw

Renders what is inside as it is written, without reading it as Jx:

{% raw %}
  To render a value, write {{ value }}.
{% endraw %}

filter

Applies a filter to everything inside:

{% filter upper %}
  this text is rendered in uppercase
{% endfilter %}

macro

A small function to reuse a piece of HTML inside one component:

{% macro item(label, active=false) %}
  <li{% if active %} class="on"{% endif %}>{{ label }}</li>
{% endmacro %}

<ul>
  {{ item("Home", active=true) }}
  {{ item("About") }}
</ul>

A macro only exists in the component it is written in. To reuse something in several components, make it a component.

Whitespace

A - next to the % of a statement removes the whitespace on that side, line breaks included:

<ul>
  {%- for n in [1, 2] %}
  <li>{{ n }}</li>
  {%- endfor %}
</ul>
<ul>
  <li>1</li>
  <li>2</li>
</ul>

Differences With Jinja

Jx uses the syntax of Jinja, but it is not Jinja. These are the things that work differently or don't exist.

Not Available

In Jinja In Jx
{% include %}, {% extends %}, {% block %} Use components and layouts
{% call %} Use the content or the slots of a component
{% with %} Use {% set %}
{% set name %}...{% endset %} Only {% set name = value %}
namespace() A {% set %} in a loop already changes the outer variable
{% break %}, {% continue %} Add a condition to the loop: {% for x in items if x.ok %}
Extensions Use custom tags

Different

  • A name that doesn't exist is an error, not an empty value. See Names.
  • There are no tests. In Jinja, x is odd or x is defined are tests. In Jx, is is the one of Python: use it for x is none and x is not none. For the rest, write the condition: x % 2 == 1, x in items, is_defined(x).
  • Some filters have other names, the ones of Python: len instead of length or count, and str instead of string. Others don't exist, like selectattr or rejectattr: add a condition to the loop. See the list of filters.
  • A mistake is found earlier. A filter that doesn't exist, a component that was not imported or an argument that is missing are errors when the component is compiled, not when it is rendered.