Jinja2 Documentation and Custom Filters Guide
Introduction to Jinja2
Jinja2 is a modern and designer-friendly templating language for Python, modeled after Django’s templates. It is fast, widely used, and secure with the optional sandboxed template execution environment.
Official Documentation
For comprehensive details, you can refer to the official Jinja2 documentation:
Custom Filters
For more advanced data management and date manipulation, Alcmeon already implemented the following custom filters.
parse_iso_date
- Can be applied on an ISO date and return a Python datetime object.
- Supports any format that is allowed by Python datetime.fromisoformat() function. See Documentation
date = '2011-11-04T00:05:23Z' | parse_iso_date
date_now
- No input required. Return the date of the execution as a Python datetime object.
now = '' | date_now
add_days
- Must be applied on a Python datetime and need a number of days to add. Works with negative number of days if you want to substract days.
updated_date = date | add_days(4) # Add 4 days to the variable 'date'
datetime
- Formats a timestamp to a readable date and time string.
- Takes an optional format input. Default format is %Y-%m-%d @ %H:%M.
"1745392360" | datetime
"1745392360" | datetime("%Y-%m-%d %H:%M:%s")
Output:
2025-04-23 @ 07:12
2025-04-23 07:12:40
date
- Formats a timestamp to a readable date string.
- Takes an optional format input. Default format is %Y-%m-%d.
"1745392360" | date
"1745392360" | date("%Y %m %d")
Output:
2025-04-23
2025 04 23
ddatetime
- Formats a Python datetime object to a readable date and time string.
- Takes an optional format input. Default format is %Y-%m-%d @ %H:%M.
datetime_obj | ddatetime
datetime_obj | ddatetime("%Y-%m-%d %H:%M:%s")
Output:
2025-04-23 @ 07:12
2025-04-23 07:12:40
ddate
- Formats a Python datetime object to a readable date string.
- Takes an optional format input. Default format is %Y-%m-%d.
datetime_obj | ddate
datetime_obj" | ddate("%Y %m %d")
Output:
2025-04-23
2025 04 23
datetime_diff
- Calculates the difference between the current datetime and a given timestamp, returning a tuple with the unit and value.
"1745392360" | datetime_diff
Output one of:
('s', 30)
-> 30 seconds of difference
('m', 12)
-> 12 minutes of difference
('h', 3)
-> 3 hours of difference
('d', 14)
-> 14 days of difference
If the difference is less than 1 minute, it is displayed in seconds. If the difference is between 1 minute and 1 hour, it is displayed in minutes. For differences between 1 hour and 1 day, it is displayed in hours. For differences greater than 1 day, it is displayed in days.
datetime_diff
- Calculates the difference in days between the current date and a given timestamp.
"1745392360" | datetime_diff
Output one of:
1
<= 1 day difference
14
14 days of difference
addslashes
- Escapes special characters in a string.
"Hello\nWorld" | addslashes
Output: Hello\\nWorld
domain_from_url
- Extracts the domain from a URL.
"https://www.example.com/path" | domain_from_url
Output: www.example.com
render_links
- Converts URLs in a string to clickable HTML links.
"Visit https://www.example.com for more info." | render_links
Output: Visit <a href="https://www.example.com" target="_blank">https://www.example.com</a> for more info.
json_escape
- Escapes special characters for use in JSON strings.
"{"key": "v/al'ue"}" | json_escape
Output: {\"key\": \"v\/al\'ue\"}
escape_url
- URL-encodes a string.
"Hello World" | escape_url
Output: Hello+World
str
- Converts a number to a string.
{{ 10 | str }}
Output: "10"
to_int
- Converts a string to a number.
{{ "10" | str }}
Output: 10
lower
- Converts a string to lowercase.
{{ "ABC" | lower }}
Output: abc
Updated about 24 hours ago