Text templates¶
Piglet includes a text-based templating language, combining the best of Jinja2, Twig and Kajiki.
Syntax¶
Statements are enclosed in {% ... %}
, and interpolations in ${..}
:
{% for line in address %}
${line}
{% end %}
Available directives are the same as for the HTML templating language, see Directives reference for details.
Whitespace control¶
If a {% … %}
tag is on a line by itself,
then that line is removed from the output.
If a {% … %}
tag is at the end of a line with other content,
the first newline after the tag is removed.
For example, this:
Twas brillig
{% if toves %}
And the slithy toves
{% end %}
Did gyre and gimble
In the wabe
Becomes this:
Twas brillig
And the slithy toves
Did gyre and gimble
In the wabe
The symbols +
or -
can be placed at the start or end of a tag to
cause the corresponding whitespace to be stripped or preserved, so:
One
{%- if two -%}
Two
{%- end -%}
Three
Becomes:
OneTwoThree
Similarly,
Four
{%- if five %} Five {% end -%}
Six
Becomes
Four Five Six
Directives¶
Most directives are the same as the HTML template directives, and are
terminated with an {% end %}
statement.
Extends, block¶
{% extends %}
and {% block %}
are used to implement template inheritance.
See py:extends for details:
{% extends "path/to/template.txt" %}
{% block intro %}
Custom intro text
{% end %}
{% end %}
ignore-missing
can be used to supress
TemplateNotFound
exceptions:
{% extends "$master_template" ignore-missing %}
{% block intro %}
Custom intro text
{% end %}
{% end %}
Include¶
{% include "info.txt" %}
ignore-missing
can be used to supress
TemplateNotFound
exceptions:
{% include "$content_path" ignore-missing %}
Import¶
{% import "path/to/template.txt" as common %}
${common.header()}
for¶
{% for i in range(5) %}
$i
{% end %}
if…else¶
{% if foo is None %}
this
{% else %}
that
{% end %}
def¶
{% def format_amount(x) %}
{% if amount >= 0 %}
${amount}€
{% else %}
(${amount}€)
{% end %}
{% end %}
with¶
{% with x = 5 %}
{% for i in range(x) %}
$x
{% end %}
{% end %}
trans, transname¶
Used to mark up translated text and dynamic value placeholders.
{% trans %}
hello {% transname name %}$user.get_full_name(){% end %}
{% end %}