Template Engine

Azu uses Crinja, a Jinja2-compatible template engine.

Basic Syntax

Variables

Output variables with {{ }}:

<h1>{{ title }}</h1>
<p>{{ user.name }}</p>
<p>{{ items[0] }}</p>

Tags

Control flow with {% %}:

{% if user %}
  <p>Hello, {{ user.name }}</p>
{% endif %}

Comments

{# This is a comment #}

{#
  Multi-line
  comment
#}

Control Structures

if / elif / else

for

Loop over collections:

Loop Variables:

Variable
Description

loop.index

Current iteration (1-indexed)

loop.index0

Current iteration (0-indexed)

loop.first

True on first iteration

loop.last

True on last iteration

loop.length

Total number of items

loop.revindex

Iterations until end (1-indexed)

for with conditions

Template Inheritance

extends

Extend a base template:

block

Define overridable blocks:

super

Access parent block content:

Includes

include

Include another template:

include with context

include with ignore missing

Macros

Define macros

Use macros

Import macros

Filters

Filters transform values:

String Filters

Filter
Description
Example

upper

Uppercase

`{{ "hello"

lower

Lowercase

`{{ "HELLO"

capitalize

Capitalize first

`{{ "hello"

title

Title case

`{{ "hello world"

trim

Remove whitespace

`{{ " hello "

truncate(n)

Truncate to n chars

`{{ text

replace(a, b)

Replace substring

`{{ name

striptags

Remove HTML tags

`{{ html

Number Filters

Filter
Description
Example

abs

Absolute value

`{{ -5

round

Round number

`{{ 3.7

round(n)

Round to n decimals

`{{ 3.14159

List Filters

Filter
Description
Example

length

Get length

`{{ items

first

First item

`{{ items

last

Last item

`{{ items

join(sep)

Join with separator

`{{ items

sort

Sort list

`{{ items

reverse

Reverse list

`{{ items

Escape Filters

Filter
Description

escape / e

HTML escape

safe

Mark as safe (no escape)

urlencode

URL encode

Default Filter

Chaining Filters

Tests

Test values with is:

Available Tests

Test
Description

defined

Variable is defined

undefined

Variable is undefined

none

Value is nil

empty

Collection is empty

even

Number is even

odd

Number is odd

string

Value is string

number

Value is number

iterable

Value is iterable

Operators

Comparison

Logical

Math

String Concatenation

In Operator

Whitespace Control

Remove whitespace with -:

Raw Output

Disable processing:

See Also

Last updated

Was this helpful?