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
F1within the editor to see menu and shortcut optionsPress
Ctrl + Spaceto 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.
Variables prefixed with ORG are related to data and functions specific to the Organization the workflow is running for. Below are the options you can call with the ORG prefix
ORG.HAS_TAG
Description: Checks if the Organization associated with the running workflow has a specific tag.
Usage:
{{ ORG.HAS_TAG.Advanced_Security }}Note: Replace spaces in tag names with underscores (
_).
ORG.VARIABLES
Description: Custom variables associated with the Organization.
Usage:
{{ ORG.VARIABLES.variable_name }}
ORG.MAPPING
Description: A mapping table that correlates Organization identifiers in the current system with those in external systems.
Usage:
{{ ORG.MAPPING.ms_tenant_id }}
ORG.INTEGRATIONS
Description: Access integration-specific data and functionality.
Usage:
{{ ORG.INTEGRATIONS.microsoft_graph }}
ORG.ATTRIBUTES
Description: Houses attributes like
idandmanaging_org_idof the organization.Usage:
{{ ORG.ATTRIBUTES.id }}
TASKS variables reference previous tasks by name.
Usage Example:
{{ TASKS.list_tickets.result.result[0].id }}Note: Replace spaces in task names with underscores (
_).
TASKS_RESULT_DATA
Description: Shortcut to reference task result data, equivalent to
{{ TASKS.task_name.result.result }}.Usage:
{{ TASKS_RESULT_DATA.Send_Message }}Special Cases: For 'with items' tasks, use
.collected_resultsinstead of.result.result.
UTILS.NOW
Description: Returns the current time based on specified timezone and format. By default, returns the current UTC time as an integer.
Usage:
{{ UTILS.now() }}or{{ UTILS.now('EST', '%d-%m-%Y %H:%M:%S') }}Format String: Consult Python's strftime format codes for custom date-time formatting.
UTILS.uuid4
Description: Generates a new UUID (Universally Unique Identifier).
Usage:
{{ UTILS.uuid4 }}
Used to reference attributes and properties of the "current workflow", the workflow that these commands are used in
WORKFLOW.human_seconds_saved
Description: Returns the value set in the Time Saved field in a workflow's settings
Usage:
{{ WORKFLOW.human_seconds_saved }}
WORKFLOW.id
Description: Returns the Id of the workflow
Usage:
{{ WORKFLOW.id}}
WORKFLOW.name
Description: Returns the name of the workflow
Usage:
{{ WORKFLOW.name}}
WORKFLOW.org_id
Description: Returns the Id of the Rewst Organization that the workflow was execute in.
Usage:
{{ WORKFLOW.org_id}}
WORKFLOW.timeout
Description: Returns the value set in the Workflow Timeout field in a workflow's settings
Usage:
{{ WORKFLOW.timeout}}
WORKFLOW.type
Description: Returns the value set in the Workflow Type dropdown in a workflow's settings.
Usage:
{{ WORKFLOW.}}
WORKFLOW.usage
Description:
Usage:
{{ WORKFLOW.}}
Reference attributes and properties of the user that ran, executed, triggered, tested, etc. a workflow.
USER.id
Description: Returns the Id of the user that executed or triggered the workflow.
Usage:
{{ USER.id }}
USER.username
Description: Returns the email address of the user that executed or triggered the workflow
Usage:
{{ USER.username }}
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:56You can convert datetime time to an epoch object. First, you set it to datetime to work with, then you can format using format_datetime.
Input:
{
2023-09-11 12:34:56
}Jinja:
{% set current = now('utc', '%Y-%m-%dT%H:%M:%SZ') %}
{{ current | format_datetime("%s") | int}}Output:
1694473606Convert the date to a weekday number (%w), which can then be used to fetch the corresponding day name from an array.
Input:
{
"date_variable": "2023-09-11"
}Jinja:
{{ ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
[CTX.date_variable | format_datetime('%w') | int] }}Output:
MondayThe time delta filter allows you to add or subtract time from a datetime variable. Here, minutes=15 adds 15 minutes, and format '%H:%M:%S' displays only the time as output.
Input:
{
"time_variable": "2023-09-11T12:34:56Z"
}Jinja:
{{ CTX.time_variable | time_delta(minutes=15) | format_datetime('%H:%M %p') }}Output:
12:49 PMAdditional 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 comprehension in Rewst's implementation of Jinja provides a compact way to transform lists, filter them, or generate new lists, without needing to end the code with {% endfor %}. It consists of three parts:
Output – What do you want in your new list?
For Loop – How do you loop through data in the original list?
Condition – Do you need to filter items to include in your new list? This is optional.
The basic structure for list comprehension is:
{{ [ output for output
in CTX.list
if condition ]}}
Iterate 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 eachuserobject in the listCTX.users.user. First_name: Retrieves thefirst_nameattribute of the currentuserobject.
In this example, the list comprehension filters CTX.users to return only the objects where the first_name attribute is "Luke."
Jinja:
{{
[
user
for user in CTX.users
if user.first_name == "Luke"
]
}}Explanation:
for user in CTX.users: Iterates over eachuserobject in the listCTX.users.if user.first_name == "Luke": Applies a filter to only include users with the first name "Luke."
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?

