Jinja essentials
Understand Jinja syntax in Rewst
Braces and their functions
Jinja uses brace delimiters to distinguish between expressions and statements. Specifically, it uses double curly braces {{ }} to surround expressions that should be replaced with output and curly braces with percent signs {% %} to surround statements that control the logic of the template. Jinja uses the {# #} syntax for comments. Anything between the opening and closing comment delimiters will be ignored by Jinja and not included in the output.
Output Values (
{{ }}): Display variables or expressions. For instance,{{ CTX.user_id }}in Rewst 102 shows user-specific data.Code Blocks (
{% %}): Used for control structures likeif,else,forloops.Comments (
{# #}): Enable non-executable notes for clarity.
Use the Monaco Editor in Rewst
The Monaco Editor is a powerful code editor that allows you to edit and preview Jinja templates in real-time. Access the Monaco Editor in many places in the product, whenever you see the clickable
next to a field or menu section.

Here are some features and keystrokes you can use with the Monaco Editor in Rewst:
Syntax highlighting: The Monaco Editor provides syntax highlighting for Jinja templates, making it easier to read and understand your code.
Code completion: The Monaco Editor provides code completion for Jinja templates, suggesting possible completion options as you type.
Find and Replace: The Monaco Editor provides a powerful Find and Replace feature that allows you to quickly search for and replace text within your code.
Keyboard shortcuts: The Monaco Editor provides a variety of keyboard shortcuts that can help you to work more efficiently. For example:
Ctrl + Space: Trigger code completion
Ctrl + /: Toggle line commenting
Work with JSON
Format: JSON (JavaScript Object Notation) structures data in key-value pairs.
Example:
{
"name": "Han Solo",
"age": 34,
"favorites": ["spaceships", "adventures"]
}Create variables in Jinja
Generally, variables are labeled containers for data that you want to use in your workflow. Variables store task results, facilitate dynamic content generation, and enhance the readability and maintainability of Jinja templates. For example, instead of updating every user's individual name in multiple places, use a variable of CustomerName to hold whatever name is needed instead, seamlessly updating as needed in every place the variable is used.
What is The Context?
The Context is a central storage space that keeps track of all data generated, captured, or used throughout a workflow. Think of it as a shared memory for a specific workflow, containing all that workflow's information.
Variable management with Jinja
Variables in Rewst are specifically referred to as data aliases. Data aliases defined within a workflow are prefixed with CTX for context variable, and stored in The Context. There are several variable types in Jinja. Learn more about them here.
Use context (CTX) variables in data aliases on your task's transitions to capture specific elements from the JSON data produced by your workflow's tasks. This is pivotal for storing and manipulating workflow data.
These types of variables can be created and modified by workflow tasks but are not global. Their scope is confined to the workflow.
Conditional statements
Jinja supports conditional statements like if, else and elif. These statements allow you to create dynamic workflows based on specific conditions, ensuring the workflow adapts to varying scenarios.
Examples:
The if statement allows you to control the flow of your template based on certain conditions. If the first condition is true, expression 1 will be executed. If the first condition is false and the second condition is true, expression 2 will be executed. If both conditions are false, expression 3 will be executed.
{% if condition %}
expression 1
{% elif condition2 %}
expression 2
{% else %}
expression 3
{% endif %}{% if rooster_sound == 'cock-a-doodle-doo' %}
<p>Time to rise and shine!</p>
{% elif rooster_sound == 'moo' %}
<p>The rooster is having an identity crisis. Someone check the barnyard!</p>
{% else %}
<p>The rooster is trying out new sounds. Perhaps it's starting a band!</p>
{% endif %}{% if user_is_looged_in %}
<p>Welcome, {{ user_name }}!</p>
{% else %}
<p>Please log in to continue.</p>
{% endif %}For loops
For loops in Jinja enable you to iterate through JSON lists, executing actions for each item. The pointer, such as thing, points to items within the list, facilitating dynamic data processing.
Examples:
In this example, the for loop iterates over a list called items and prints each item to the output. The item variable represents the current item in the list being iterated over. The tag marks the end of the loop.
{%- set items = ["crawfish", "butter", "rice", "garlic", "onions"] -%}
{% for item in items %}
{{ item }}
{% endfor %}{% for thing in CTX.list_of_things %}
<li>{{ thing }}</li>
{% endfor %}Jinja filters
For more detailed information on Jinja filters, see our list here.
Jinja filters are functions that can be applied to variables and expressions within Jinja templates to modify their output. They are used to perform a wide range of data manipulations, such as formatting strings, converting data types, and filtering lists. Some commonly used filters include upper, lower, title, default, join, and random. Jinja also allows for the creation of custom filters to meet specific needs. Overall, filters are a powerful tool that can help you to manipulate data more easily and efficiently within your Jinja templates
Examples:
Truncate text:
{{ text|truncate(20) }}Capitalize names:
{{ user_name|capitalize }}Lowercase and replace text:
{{ user_email|lower|replace("@", "at") }}
Types of Jinja
Variable expressions
Variable expressions are encapsulated by double curly braces ({{ and }}) and will output the value of the variable or expression as they are evaluated.
{{ CTX.my_var }}
Control flow statements
These are used for decision-making functions such as sets, if statements and for eaches. They are encapsulated by curly+percent signs ({% %})
{% set x = 100 %}These statements typically do not output anything.
Comments
Functionally, comments do nothing. In the Rewst Monaco editor, you can use the hotkey CTRL-/ to comment blocks of code.
{# COMMENT #}
Whitespace
By default, when Jinja begins a statement block, it preserves any whitespace characters such as spaces, carriage returns, etc., before or after the block. In many cases, you'll want to remove any spaces you did not explicitly intend to have, so the addition of the - character in the open and closing braces will remove the whitespace before or after the statement, respectively.
{%- for part in parts_list -%}
{{- part.name -}}
{%- endfor -%}Advanced Jinja in Rewst
List comprehension in action
Functionality: Efficiently creates new lists from existing ones, based on specific criteria. You can combine filters and conditions to produce concise, targeted lists, all in one Jinja expression.
“Give me a list of all X from Y, but only if Z.”
Application: Tailors data selection in workflows, enhancing efficiency and precision.
Three-step structure
List comprehension combines filters and conditions to create concise and targeted lists. This structure ([item for item in list if condition]) allows you to filter data efficiently.
Output: Define what you want to extract or manipulate (e.g.,
user.id).Construction: Specify the list to iterate over (e.g.,
CTX.my_user_list).Conditions: Apply conditions to filter data (e.g.,
if user.enabled == true).
Example:
{{
[
user.id
for user in CTX.my_user_list
if user.enabled == true
]
}}This example demonstrates filtering active users from CTX.my_user_list.
List comprehension with conditions
Purpose: Allows you to filter data based on specific criteria.
For example, using filters like
lowerto standardize data before comparison.
Benefit: Ensures more accurate and relevant data processing, critical for complex workflows.
List comprehension with math
Purpose: This method allows you to apply specific mathematical functions to each element in a list, thereby modifying the output based on your needs.
Benefit: Offers a succinct method to apply mathematical transformations across a list, yielding a new list with modified values.
Example:
squared_numbers = [num * num for num in list_of_numbers]If list_of_numbers = [1, 2, 3, 4], then the squared_numbers list after applying the above list comprehension will be [1, 4, 9, 16].
This concise approach efficiently applies the squaring function to each item in the original list and collects the results in a new list.
Last updated
Was this helpful?

