Writing Conditional Statements

Text templates can conditionally produce text for input to a brick.

Condition Expression

If there are only 1 or 2 simple cases, you can write the condition inline as an expression:

{{ "Meow" if @animal == "cat" else "Woof!" }}

Using Text Templates for Brick Conditions

There are two ways to conditionally run a brick:

The condition can be a variable, or a text expression evaluating to true, t, yes, y, on, or 1.

For example:

{{ true if @count > 0 }}
{{ true if not @myVariable }}

Comparison and Logic Expressions

PixieBrix text templates support comparison and logical operators in expressions:

  • == , !=: equals and not equals

  • >, <, >=, <=: numeric comparison

  • and , or: logic

  • not: negation

Here’s an example showing comparison and logic in an expression:

circle-exclamation

Condition Tag Blocks

If there are multiple cases, or one or more cases are multi-line, you can use condition blocks to provide the cases:

As you can see, blocks are designated using {% and %} instead of the {{ mustache braces. The tags are if, elif, and endif.

circle-exclamation

You can learn more about the if tag in the Nunjucks Template Language documentationarrow-up-right.

Last updated

Was this helpful?