Internal Rewst Jinja examples
Shortcuts and general help
Access the Jinja Editor on any fields where you see the code editor icon
(fondly called the Jinja Burger amongst other Kewp Members)
Press
F1
within the editor to see menu and shortcut optionsPress
Ctrl + Space
to get the initial root optionsAdd
|
to the end of your variables to access Jinja filters. e.g.:{{ ORG.ATTRIBUTES.id|default('test default string') }}
Variable roots
Context Variables are specific to the currently executing workflow. They include variables like inputs from the workflow's trigger, any defined Data Aliases, and results from tasks that have been executed.
Usage:
{{ CTX.variable_name }}
Additional Note: Task results can be accessed once the task is complete. If attempted earlier, the variable will be
undefined
.
Date-time operations
Date-Time manipulations in Jinja can be easily achieved using context variables and built-in filters. This section provides examples to perform various date-time operations with JSON objects. Here are various examples demonstrating how to manipulate date-time values using Jinja filters in Rewst.
Examples
You can convert epoch time to a datetime object using convert_from_epoch
, then format it using format_datetime
.
Input:
{
"epoch_time": 1694473606
}
Jinja:
{{ CTX.epoch_time|convert_from_epoch|format_datetime('%Y-%m-%d %H:%M:%S') }}
Output:
2023-09-11 12:34:56
Additional resources
The format_datetime
filter leverages format codes to specify the output string's structure. Here is a breakdown of commonly used format codes:
%Y
: 4-digit year (e.g., 2023)%y
: 2-digit year (e.g., 23)%m
: Month as a zero-padded decimal (e.g., 09 for September)%B
: Full month name (e.g., September)%d
: Day of the month as a zero-padded decimal (e.g., 11)%A
: Full weekday name (e.g., Monday)%w
: Weekday as a decimal number, where Sunday is 0 and Saturday is 6%H
: Hour (24-hour clock) as a zero-padded decimal (e.g., 14)%I
: Hour (12-hour clock) as a zero-padded decimal (e.g., 02)%M
: Minute as a zero-padded decimal (e.g., 34)%S
: Second as a zero-padded decimal (e.g., 56)%p
: AM or PM
For more context and assistance with date-time formats, consult this Python strftime cheatsheet.
Rewst's custom Jinja2 extensions
In your Rewst environment, you have access to several custom Jinja2 extensions that enhance the functionality of Jinja templates in Rewst. These extensions provide additional features and capabilities for your templating needs.
Date and time handling
Purpose: This extension allows you to work with date and time values directly within your Jinja2 templates.
Usage:
{{ now }}
: Inserts the current date and time.{{ now, "%Y-%m-%d" }}
: Inserts the current date and time formatted according to the specified format (e.g.,2023-11-22
).{{ now, False }}
: Inserts the current timestamp.
Example:
Current Time: {{now("UTC")|convert_from_epoch|}} Formatted Time: {{ now("UTC")|convert_from_epoch|format_datetime("%I:%m:%S %p) }} Timestamp: {{ now("UTC") }}
Try and catch blocks
Purpose: The Try Catch extension introduces try and catch blocks in your Jinja2 templates, allowing you to handle exceptions gracefully.
Usage:
{{ try }}
: Defines a try block where you can place code that may raise exceptions.{{ catch }}
: Optionally defines a catch block to handle exceptions. If no catch block is specified, an empty string is returned in case of an exception.
Example:
{{ try }} The thing: {{ i_do_not_exist_and_throw_an_error }} {{ catch }} Error: {{ exception }} {{ endtry }}
UUID generation
Purpose: The UUID extension simplifies the generation of Universally Unique Identifiers (
UUIDs
) within your Jinja2 templates.Usage:
{{ uuid4 }}
: Inserts a randomly generated UUID version 4.
Example:
Generated UUID: {{ uuid4 }}
List comprehension
List comprehensions in Rewst's implementation of Jinja provide a compact way to transform lists, filter them, or generate new lists.
Iterating through a list
{{ [users.userPrincipalName for users in TASKS.task_name.result.result ]}}
Result: This will return all the users property "userPrincipalName"
Iterating through a list and finding a specific value
{{ [users.userPrincipalName for users in TASKS.task_name.result.result if users.displayName == "Test Name"]}}
Result: This will return the userPrincipalName of the user(s) that has a display name of "Test Name"
{{ [{"group_name": groups.displayName, "group_id": groups.id} for groups in TASKS.task_name.result.result if (not groups.dynamicMembership)"]}}
Result: This will return an object with the specified keys (group_name and group_id) and the specified property. It will only show groups that do not have a "dynamicMembership" property. This means that it will remove all dynamic groups from the list.
{{ [{"group_name": groups.displayName, "group_id": groups.id} for groups in TASKS.task_name.result.result if (not groups.dynamicMembership) and (groups.displayName == "Testing Group Name"]}}
Result: This done the same as the above, except it has two conditions - adding that it'll only return groups with a display name of "Testing Group Name"
List comprehension examples
This section breaks down two practical examples of how to leverage list comprehensions in your Jinja templates: returning a new list of only a specified attribute, and filtering a list by the existence of a specific value within the specified attribute. Check out the details below.
This example demonstrates how to extract the first_name
attribute from each user
object within the CTX.users
list. The list comprehension iterates over CTX.users
and retrieves the first_name
for each entry.
Jinja:
{{
[
user.first_name
for user in CTX.users
]
}}
Explanation:
for user in CTX.users
: Iterates over eachuser
object in the listCTX.users
.user. First_name
: Retrieves thefirst_name
attribute of the currentuser
object.
These custom Jinja2 extensions enhance your templating capabilities, enabling you to work with dates, handle exceptions, generate UUIDs and perform List Comprehension seamlessly within your Rewst environment. Whether you need to format dates, gracefully handle exceptions, or generate unique identifiers, or perform comprehensions, these extensions provide the tools you need to streamline your templating tasks.
Last updated
Was this helpful?