How to Format Jinja Templates
This guide describes the fundamental of Jinja templating and how to format it within your agents. Learn more about:
Overview
Jinja is a powerful templating language that allows you to create dynamic content by embedding logic and variables directly into your Block Agent. Instead of writing static, one-size-fits-all configurations, you can create intelligent, context-aware instructions that adapt based on run-time data.
Benefits of Using Jinja Templates
-
Dynamic personalization:
-
Adapt agent personality based on brand guidelines.
-
Customize greetings based on customer history.
-
Tailor responses to patient status (new vs. returning).
-
-
Conditional logic:
-
Show different content based on data availability.
-
Handle multiple scenarios within a single turn.
-
Create branching conversational flows.
-
-
Data-Driven Instructions:
-
Access appointment data from external systems.
-
Display available time slots dynamically.
-
Reference customer information in prompts.
-
-
Maintainability:
-
A single template supports multiple scenarios.
-
Centralized logic reduces duplication.
-
Policy-driven behavior changes without code updates.
-
-
Reusability
-
Create reusable agent components.
-
Share templates across multiple implementations.
-
Standardize patterns across your AI platform.
-
Jinja Concepts
You can include Jinja templating in a variety of ways within your agent. It can be as simple as a variable for a company name, or as complex as filters that indicate a caller has an appointment coming up today so we should greet them differently.
Variables
Variables are enclosed in double curly braces and are replaced with their actual values at run-time.
Syntax
{{ variable_name }}
Sample Jinja Variable
Welcome to {{ arg_brand_name }}! Today's date is {{ arg_context_date_time }}.
The output from this variable might look like this:
Welcome to Dr. Teeth and Associates Dentistry! Today's date is 2025-12-11.
Confirm Variable Existence
Note: We recommend always checking if a variable exists before using it to avoid errors.
Syntax
{% if variable is defined %}
{{ variable }}
{% endif %}
Sample Jinja Variable Existence Confirmation
{% if arg_existing_appointments is defined and arg_existing_appointments.matches is defined %}
You have {{ arg_existing_appointments.matches|length }} upcoming appointments.
{% else %}
I don't see any upcoming appointments in our system.
{% endif %}
Conditional Statements
Control what content appears based on conditions using if, elif (else if), and else.
Syntax
{% if condition %}
content when true
{% else %}
content when false
{% endif %}
Sample Jinja Conditional Statement
{% if arg_ani_match %}
Welcome back! I see you've been here before.
{% else %}
Welcome! Is this your first time calling?
{% endif %}
Loops
Iterate over collections of data like appointment lists or insurance providers.
Syntax
{% for item in collection %}
Process {{ item }}
{% endfor %}
Sample Jinja Loop
Here are your upcoming appointments:
{% for appointment in arg_existing_appointments.matches %}
- Appointment on {{ appointment.appointment_date }}
{% endfor %}
Filters
Transform data using pipe operators.
Commonly used filters include:
-
|length: Get collection size
-
|upper: Convert to uppercase
-
|lower: Convert to lowercase
-
|join(', '): Join list with delimiter
-
|int: Convert to integer
-
|format(): String format
Sample Jinja Filter
{% if arg_existing_appointments.matches|length > 0 %}
You have {{ arg_existing_appointments.matches|length }} appointments.
{% endif %}
Comments
Induce non-rendered notes for documentation or your teammates.
Syntax
{# This is a comment and won't appear in output #}
Now that you've learn about how to format Jinja concepts, check out this page for how to integrate Jinja templating within a Block Agent.