Overview Jinja Templating
1. Introduction to Jinja
Jinja is a templating language for dynamically generating content, commonly used in Python web frameworks like Flask and Django, as well as in YAML files for configuration automation.
Why Use Jinja?
Dynamic Variables – Insert values into templates without hardcoding.
Conditional Logic – Create templates that adapt based on conditions.
Loops & Filters – Process lists, format data, and apply transformations.
Used in YAML, HTML, JSON, and more – Supports dynamic configuration files.
Basic Jinja Syntax
Jinja expressions are enclosed in double curly braces ({{ }}
) for output, and curly braces with percentage signs ({% %}
) for logic.
- Variables:
{{ variable }}
- Conditions:
{% if condition %} ... {% endif %}
- Loops:
{% for item in list %} ... {% endfor %}
- Filters:
{{ variable | filter }}
2. Using Jinja in YAML Configuration Files
Jinja is commonly used in YAML for configuring automation, API requests, and templated configurations.
Example: Inserting Variables
api_url: "https://`{{ env.API_HOST }}`"
Example: Using Jinja for Dynamic API Requests
providers:
- id: "http"
config:
raw_request: |
POST /detect/jailbreak HTTP/1.1
Host: `{{ ENV_HOST }}`
Content-Type: application/json
{
"texts": [
"`{{ prompt }}`"
]
}
3. Jinja Expressions and Filters
Variables in Jinja
greeting: "Hello, `{{ user_name }}`!"
4. Jinja Filters
Common Jinja Filters
| Filter | Description | Example | Output |
|-------------|--------------------------------------|--------------------------------------------------------|----------------|
| **lower** | Converts to lowercase | `{{ "HELLO" | lower }}` | "hello" |
| **upper** | Converts to uppercase | `{{ "hello" | upper }}` | "HELLO" |
| **title** | Capitalizes first letter of each word| `{{ "hello world" | title }}` | "Hello World" |
| **replace** | Replaces part of a string | `{{ "error: 404" | replace("error", "status") }}` | "status: 404" |
| **default** | Default if variable missing | `{{ undefined_var | default("N/A") }}` | "N/A" |
| **join** | Joins list to string | `{{ ["one", "two", "three"] | join(", ") }}` | "one, two, three" |
| **length** | Gets length of string or list | `{{ "Hello" | length }}` | `5` |
| **int** | Converts to integer | `{{ "42" | int }}` | `42` |
| **round** | Rounds numbers | `{{ 3.14159 | round(2) }}` | `3.14` |
Example in YAML
server_name: "`{{ 'myServer' | upper }}`"
5. Jinja Conditionals (if
)
api_host: "{% if env.ENVIRONMENT == 'production' %}api.prod.com{% else %}api.dev.com{% endif %}"
6. Jinja Loops (for
)
Iterating Over a List
services:
{% for service in ["database", "cache", "api"] %}
- "`{{ service }}`"
{% endfor %}
Iterating with Index
users:
{% for user in ["Alice", "Bob", "Charlie"] %}
- name: "`{{ user }}`"
id: "`{{ loop.index }}`"
{% endfor %}
7. Nested Jinja
servers:
{% for region in ["us-east", "us-west", "europe"] %}
- region: "`{{ region }}`"
host: "{% if region == 'europe' %}eu.server.com{% else %}us.server.com{% endif %}"
{% endfor %}
8. Jinja in JSON (APIs)
{
"users": [
{% for user in ["Alice", "Bob", "Charlie"] %}
{
"name": "`{{ user }}`",
"id": "`{{ loop.index }}`"
}{% if not loop.last %},{% endif %}
{% endfor %}
]
}
9. Combining Jinja + JQ
providers:
- id: "http"
config:
raw_request: |
POST /api/detect HTTP/1.1
Host: `{{ ENV_HOST }}`
Content-Type: application/json
{
"texts": [
"`{{ prompt }}`"
]
}
JQ for Response Transformation
transform_response: |
{
"JAILBREAK": json["results"][0]["chunk_results"][0]["JAILBREAK"],
"INJECTION": json["results"][0]["chunk_results"][0]["INJECTION"]
}