Welcome to Rewst 103! In this course, we'll be talking about Jinja, the templating language that has become an essential tool for web developers and data engineers alike. In this article, we will explore the basics of Jinja, its syntax, and various use cases, with a focus on understanding the different use cases.
Try out filters like | flatten or | sort to organize.
Render the code.
Review the Output.
Re-add the comments.
Step 5: Use More Advanced List Comprehension and Filtering
Check for a Specific Attribute
Uncomment and Render the following:
{{-
[
weekday.day
for weekday in CTX.days
if weekday.lunch.drink == "Milk"
]
-}}
Review to see that it only shows Monday even though Friday has Chocolate Milk.
Re-add the comments.
Ensure all Versions of the Attribute are Displayed
Uncomment and Render the following:
{{-
[
weekday.day
for weekday in CTX.days
if "milk" in weekday.lunch.drink | lower
]
-}}
Review to see that the output includes both options.
Add String Concatenation to Turn Data into a Sentence
Replace the top line of this code snippet to say weekday.lunch.drink ~ " will be served on " ~weekday.day.
Render the following:
{{-
[
weekday.lunch.drink ~ " will be served on " ~weekday.day
for weekday in CTX.days
if "milk" in weekday.lunch.drink | lower
]
-}}
Review the output.
Use the Join Operator to Combine the Two Statements
Add| join(" and ") to the end of the code snippet.
Render the following:
{{-
[
weekday.lunch.drink ~ " will be served on " ~weekday.day
for weekday in CTX.days
if "milk" in weekday.lunch.drink | lower
] | join(" and ")
-}}
Review the output.
Re-add the comments.
Multiply numbers within a list by themselves, and output a new list with modified data
Uncomment & Render the following:
{% set old_list = [1,2,3] %}
{{
[
num * num for num in old_list
]
}}
Step 6: Combine List Comprehension and Loops
Build a List and Loop Through it
Uncomment and Render the following:
{%- set milky_days = [
weekday
for weekday in CTX.days
if "milk" in weekday.lunch.drink | lower
]
-%}
{%- for weekday in milky_days -%}
On {{ weekday.day }} you should bring a frosty RewstyCola for lunch at {{ weekday.lunch.time }} because they are serving {{ weekday.lunch.drink | lower }}.
Review the output.
Re-add the comments.
Eggstra-Credit!
Eggstra-Credit!
Loop Through and If And Statement and Combine with a Join Operator
Uncomment and Render the following:
On {{
[
weekday.day
for weekday in CTX.days
if
"west" in weekday.weather.wind_direction
and
(
"rain" in weekday.weather.conditions | lower
or
"storm" in weekday.weather.conditions | lower
)
] | join(" and ")
}} you will need to close the darn windows!
Review the output.
Re-add the comments to revert the code.
Loop Through and Output JSON Objects
Uncomment and Render the following:
{{-
[
{
"day": weekday.day,
"temp": weekday.weather.temperature
}
for weekday in CTX.days
if
weekday.weather.temperature <= 65
]
-}}
Review the output.
Add a comma at the end of "temp": weekday.weather.temperature.
Type"lunch": weekday.lunch on the next line.
Render the following:
{{-
[
{
"day": weekday.day,
"temp": weekday.weather.temperature,
"lunch": weekday.lunch
}
for weekday in CTX.days
if
weekday.weather.temperature <= 65
]
-}}
Review the output to see the lunch attribute added.
Re-add the comments to revert the code.
Output an HTML Table
Uncomment the HTML structure at the bottom of your sample code.
Render the HTML Structure.
Open a text editor and copy/paste the output.
Save the file as Jinja-to-html.html.
Drop the file into a browser to see the results!
Conclusion
Understanding Jinja and its diverse functionalities empowers trainers to create dynamic and responsive workflows within the Rewst environment. By mastering Jinjaβs syntax, you can efficiently manipulate data, enabling the seamless execution of complex tasks.
Additional Resources
For more information on using Jinja, check out our documentation: