Template Variables Tutorial
Introduction
In the world of CrewAI, templates play a crucial role in dynamically generating content. The backbone of these templates is the use of template variables. This tutorial will take you through the concept of template variables, how to use them, and provide practical examples to solidify your understanding.
What are Template Variables?
Template variables are placeholders within a template that are replaced by actual data when the template is rendered. They allow you to create dynamic and reusable content. When the template is processed, the variables are substituted with actual values provided in the context.
Basic Syntax
The basic syntax for template variables typically involves wrapping the variable name in double curly braces {{ }}
. For example, {{ variable_name }}
.
Example:
{{ user_name }}
Example Usage
Let’s consider a simple example where we have a template that greets a user by name. The variable {{ user_name }}
will be replaced with the actual user's name when the template is rendered.
Template:
Hello, {{ user_name }}!
Assuming the context provided is { user_name: "John" }
, the rendered output would be:
Hello, John!
Using Multiple Variables
You can use multiple variables within a single template to create more complex and dynamic content. Here’s an example:
Template:
Hello, {{ user_name }}!
Your order number is {{ order_number }} and the total amount is {{ total_amount }}.
With context { user_name: "John", order_number: "12345", total_amount: "$150" }
, the rendered output would be:
Hello, John!
Your order number is 12345 and the total amount is $150.
Conditionals with Template Variables
Templates often need to display content conditionally based on the values of variables. This can be achieved using conditional statements. Here’s an example in a templating language that supports conditionals:
Template:
{% if user_name %}
Hello, {{ user_name }}!
{% else %}
Hello, Guest!
{% endif %}
If user_name
is provided in the context, the output will include the user's name. Otherwise, it will greet as "Guest".
Loops with Template Variables
Templates can also iterate over lists or arrays using loops. This is useful for displaying repeated content. Here’s an example:
Template:
{% for item in items %}
- {{ item }}
{% endfor %}
Assuming the context { items: ["Apple", "Banana", "Cherry"] }
, the rendered output would be:
- Apple
- Banana
- Cherry
Escaping Variables
In some cases, you might want to escape variables to prevent them from being interpreted as HTML or other code. This is crucial for avoiding security issues like XSS (Cross-Site Scripting). Here’s an example:
Template:
{{ user_input | escape }}
This ensures that any HTML tags or scripts in user_input
are treated as plain text.
Conclusion
Template variables are a powerful feature in CrewAI templates, enabling the creation of dynamic and context-sensitive content. Mastering the use of template variables, including conditionals, loops, and escaping, will greatly enhance your ability to generate versatile and secure templates. Practice with different examples and contexts to become proficient in using template variables.