In this lesson, we review context data from the form and workflow that you create in "Building a Basic Form and Workflow."
Lesson Modules
To get started, select the first module. Each module includes a video and written guidance with resources, followed by navigation to go back/forward. To wrap up the lesson, visit Next Steps.
Submit this form to get credit for completing this lesson offline.
Keep On Cluckin'
You've created a simple form, a workflow, and connected them with a trigger. In upcoming lessons, you’ll refine this automation. But first...it's time to learn the basics of Jinja (and demystify all of that "CTX.user_id," "CTX.group_id" stuff). Jump to the next lesson when you're ready!
Recognize JSON formatting in the context of the Jinja live editor + Use the Live Editor to practice Jinja code on data from the context
Module Overview
💡 Let's explore the format of JSON data in the context of the Jinja Live Editor, to visualize and experiment with data as part of Rewst workflows.
Video (1:10 Minutes)
Video (3:50 Minutes)
Key Concepts About Jinja and JSON
Jinja is the templating language used in Rewst workflows. It allows you to create placeholders in your code that are filled in with real data when the workflow runs.
Think of Jinja as your tool for working with dynamic data like user names, group IDs, and actions.
Every time a Rewst workflow runs, the data processed is shown in JSON (JavaScript Object Notation) format in the context of the workflow.
JSON organizes data into objects (also called context variables in Rewst workflows), with key: value pairs. For example, "action" is a key and "add" could be the value.
Working with Object Attributes
In JSON, objects may have additional attributes, organized within more braces. You can access a specific attribute using dot notation.
For example, to get the organization ID, use "CTX.organization.id" in the Jinja Live Editor.
If you need the entire object, ask for "CTX.organization" to get all its attributes and values.
Accessing Context Data in Rewst
To get familiar with your context data, complete these steps in Rewst:
Open the "Add or Remove User - Microsoft Group" workflow and go to the "workflow results" by selecting the graph icon.
Open the most recent success and review the Context data. This will show you all the information processed during the workflow execution.
Key objects like "action," "user_id," and "group_id" are visible as part of the context data. These were configured in the workflow input and are now accessible for use in Jinja code.
Exploring the Jinja Live Editor
The Jinja Live Editor is a great place to experiment with Jinja code. You can paste in JSON data and practice writing Jinja statements without affecting any real data.
Action Items
Read the Jinja Essentials documentation for more information on Jinja and JSON, including Rewst examples
Review the context data from your "Add or Remove User - Microsoft Group" workflow execution (part of the lesson, "Building a Basic Form and Workflow").
Navigation
Basic Jinja syntax and filters
Identify common Jinja syntax and filters + Use the Live Editor to practice Jinja code on data from the context
Module Overview
💡 It's time to tackle basic Jinja syntax and filters. These building blocks will help you write and practice Jinja code in the Jinja Live Editor, and eventually use it in your Rewst workflows.
Video (5:21 Minutes)
Key Concepts About Jinja Syntax
Output Dynamic Content:
Use two curly braces to output or print data from your workflow.
Example:{{ CTX.action }} outputs the value of the "action" variable, such as "add" or "remove."
Jinja Statements:
Use {% %} for code blocks that control logic, such as if/else statements or loops.
Example:{% if CTX.action == "add" %} The user was added{% else %} The user was removed{% endif %}
Comments:
Use {# #} to add comments in your code. Comments are ignored during execution but help others (or yourself) understand the code.
Example:{# This is a comment #}
Removing Extra Spaces:
Use a dash inside any of these delimiters to strip extra spaces.
Example:{{- CTX.action -}}
Jinja Filters
Filters modify data in Jinja without changing the original information. Common filters allow you to format dates, capitalize text, or provide default values.
Example: Use the d filter to avoid errors if a context variable is empty:
{{ CTX.group.displayName | d }} outputs an empty string if there is no display name for the group.
Action Items
Review Context Data from the "Building a Basic Form and Workflow" Lesson
Open the workflow results from your "Add or Remove User - Microsoft Group" workflow and review the context data. Practice rendering the following Jinja code in the Jinja Live Editor, and notice that the results match the values of the context data for the variables:
{{ CTX.action }}
{{ CTX.user_id }}
{{ CTX.group_id }}
Complete the Lunch Menu Exercises:
Navigate to the "Lesson Resources" section to access the "Lunch Menu" sample data set and instructions for using Jinja syntax and filters in the Jinja Live Editor. Focus on:
Rendering values from the context
Applying Jinja filters to format the output
Practicing list comprehension (using two different methods -- a Jinja statement, and list comprehension).
By practicing these exercises, you’ll solidify your understanding of Jinja syntax, filters, and data manipulation in Rewst workflows.
Navigation
Jinja list comprehension
Practice writing list comprehension, using Jinja, to create a new list from existing list data
Module Overview
💡 List comprehension (a special enhancement to "Rewst-flavored Jinja"!) allows you to create a new list from an existing one in a simplified way, 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 (Optional) – Do you need to filter items to include in your new list?
Here's the basic structure:
{{
[ output for output
in CTX.list
if condition ]}}
Video (3:16 Minutes)
Additional Tips
Use dot notation in the output to specify parts of each item.
The condition (typically, an "if" statement) is optional and filters based on criteria like "if the color is not red."
If you want to turn the list into a string, apply the "join" filter. You'll find an example of this in the "Lunch Menu" exercises for this lesson.
For a few examples, refer to the Jinja List Comprehension Examples module.
Action Items
Practice the list comprehension exercises included in the "Lunch Menu" data set: Lesson Resources
Navigation
Jinja list comprehension examples ("Them Apples")
Practice writing list comprehension, using Jinja, to create a new list from existing list data.
Module Overview
💡 Let's explore list comprehension with two examples, focusing on how the output (first part) and condition (last part, optional) can be used.
Video (4:56 Minutes)
Picture this: a basket of apples labeled "them_apples." Each apple has attributes like color, shape, and brand name (e.g., Granny Smith, Red Delicious). We’ll use these details to build two different lists.
Example 1: Apple Names
We want a list of the apple brand names. Here's how the list comprehension breaks down:
Output: The specific attribute we want is the apple's name. So, we use apple.name.
For Loop: Loop through each apple with for apple in CTX.them_apples.
Condition: No condition needed here.
The result is a list of brand names like Pink Lady, Honeycrisp, and Jazz, stored in CTX.apple_names. You’ll learn more about storing lists in the lesson on Creating an Option Generator Workflow.
Example 2: Down With Red Delicious
This time, let's filter a list by the attribute "color" to create a new list that excludes red apples.
Output: We want the whole apple this time, so we use apple.
For Loop: Same as before, for apple in CTX.them_apples.
Condition: Only include apples with the colors pink or green, by writing if "green" in apple.color or if "pink" in apple.color.
Alternative methods for the condition could be:
if "red" not in apple.color
if "Red Delicious" not in apple.name
This gives us a new list of only pink and green apples, stored in CTX.best_apples.
Action Items
Practice the Lunch Menu exercises that use list comprehension.
Post further questions about list comprehension in the #cluck-u Rewst Discord channel to get help from professors and peers!
Navigation
Using Jinja in Rewst workflows
Recognize where to use Jinja in workflows.
Module Overview
💡 In Rewst workflows, Jinja helps us work with variables in automations. You’ll see variables in places like:
Form field names
Workflow configuration settings
Action parameters (like "publish results as")
Workflow transitions (custom conditions, data aliases)
Context variables are created and modified through workflow actions and transitions, making data reusable across the workflow. Let’s break down some key workflows where Jinja plays a role.
Video (6:31 Minutes)
Jinja Throughout the Rewst Foundations Course
Here are a few examples of how and where you'll use Jinja in other course lessons:
Building a Basic Form and Workflow: Form field names become input variables (e.g., user_id, group_id, action), which are passed into the workflow context for each form submission. If you've already completed this lesson, you've used Jinja by using code in your action parameters: CTX.group_id and CTX.user_id .
Creating an Option Generator Workflow: You’ll configure output variables like {{ CTX.group_result }} for workflow output.
Workflow Logic: Custom conditions use Jinja to check variable attributes, e.g., {{ Graph }} transition.
Getting Modular with a Sub-Workflow: Input variables become required parameters when this workflow is used in a "parent workflow."
How Data is Created in a Workflow
Data is created through workflow actions in two ways:
Action Result: When the action runs, Rewst gets raw data (e.g., Microsoft Graph API response) stored in the "Result" section.
Data Alias: If a transition condition is met, data aliases can store results (e.g., success or failure).
Check out this example of a Microsoft Graph List Groups action:
Data is stored in group_list and validated by a data alias called hoorayin the "on success" transition.
If the action fails, the data alias boowould show in the workflow execution results (as it is added to the "on failure" transition).
Pro Tip: Use a data alias to store and modify data as needed. You’ll practice this in the lesson on Creating an Option Generator Workflow.
Action Items
Beyond the examples reviewed in this module, you can also use Jinja in a feature available for workflow actions, called "Publish Results As." To see this in action, watch the video on how to reference data with variables here.