Tags
apply
The apply tag lets you use filters on a block of template data:
{% apply upper %}
This text becomes uppercase
{% endapply %}This means the text "This text becomes uppercase" will be transformed to uppercase when rendered.
for
The for loop lets you iterate over each item in a sequence. Here's a deeper dive into its functionalities:
Basic Looping:
To display a list of users:
Users:
{% for user in users %}
- {{ user }}
{% endfor %}Using the .. Operator:
This operator can help you iterate over a sequence of numbers or letters:
Special Variables in for Loop:
Inside a for loop, you can access:
loop.index: Current iteration (1 indexed)loop.index0: Current iteration (0 indexed)loop.revindex: Iterations from the end (1 indexed)loop.revindex0: Iterations from the end (0 indexed)loop.first: True if it's the first iterationloop.last: True if it's the last iterationloop.length: Number of items in the sequenceloop.parent: The parent context
Iterating Over Keys or Keys and Values:
Iterating Over a Subset:
To loop over a subset of values, use the slice filter:
if
The if statement allows you to test if an expression evaluates to true.
Simple Conditions:
To test if an expression evaluates to true:
Testing Non-Empty Arrays:
Note: To check if a variable is defined, use if users is defined.
Using the not Operator:
Combining Conditions with and and or:
Multiple Branches with elseif and else:
macro
Macros can be likened to functions in most programming languages. They provide a mechanism for reusing template fragments, ensuring that there's no unnecessary repetition in your code.
Example:
Every argument passed to a macro can have a default value.
set
You have the capability to assign values to variables. These assignments utilize the set tag and can target multiple variables simultaneously.
Basic Variable Assignment:
Assign the value 'bar' to the variable foo:
Subsequently, foo becomes available in the template:
Variable Assignment with Expressions:
The value assigned can be any valid expression:
Multiple Variable Assignments:
You can assign to multiple variables simultaneously:
Capturing Chunks of Text:
The set tag can be utilized to capture chunks of text:
Loop Scope:
Loops are scoped. This means a variable declared inside a loop isn't accessible outside:
However, to make the variable available outside, you should declare it prior to the loop:
verbatim
The verbatim tag serves to delineate sections as raw text, ensuring they are not parsed. This can be particularly useful when you wish to showcase template syntax as an example within a template. Here's an illustrative snippet:
with
The with tag is utilized to create an inner scope. Any variables defined within this scope will not be visible outside of it.
Basic Usage:
Outside the block, the variable foo will no longer be accessible.
Defining Variables with a Hash:
You can also define the variables at the beginning of the with scope by passing a hash:
Again, outside the block, the variable foo will not be visible.
Using Expressions that Resolve to a Hash:
This technique works with any expression that resolves to a hash:
Limiting Visibility with the only Keyword:
By default, the inner with scope has access to the outer scope context. If you wish to limit the inner scope to only the variables defined within it, use the only keyword:
Last updated