Comment on page
Writing Conditional Statements
Text templates can conditionally produce text for input to a brick.
If there are only 1 or 2 simple cases, you can write the condition inline as an expression:
{{ "Meow" if @animal == "cat" else "Woof!" }}
Text Templates are the most convenient way to provide a Condition that controls if a single brick runs.
📖 Learn about the Brick Condition field in the Page Editor Guide
Using a Text Template condition expression to control when a brick is run.
Text Templates can also provide the condition for the If-Else brick. The If-Else brick can run multiple bricks if a condition is met.
PixieBrix text templates support comparison and logical operators in expressions:
==
,!=
: equals and not equals>
,<
,>=
,<=
: numeric comparisonand
,or
: logicnot
: negation
Here’s an example showing comparison and logic in an expression:
To provide multiple conditions using
and
or or
, provide them within the same template expression {{ true if @run > 0 and @animal == "cat" }}
If there are multiple cases, or one or more cases are multi-line, you can use condition blocks to provide the cases:
{% if @animal == "cat" %}
Meow!
{% elif @animal == "dog" %}
Woof!
{% else %}
???
{% endif %}
As you can see, blocks are designated using
{%
and %}
instead of the {{
mustache braces. The tags are if
, elif
, and endif
.Note that alternative cases use the
elif
tag with no space. I.e., as opposed to else if
which is used in many programming languages
✅ elif
❌ else if
Last modified 17d ago