Jinja Templating
AIOP allows each user to generate files from a Jinja (opens in a new tab) template. It is also possible to generate templated PDFs (learn more here). It's also possible to use FMPP instead.
Jinja is a fast, expressive, and extensible template engine. It allows fields to write code similar to Python syntax.
Usage
Jinja is simple to use. Let's look at some ways to use it. Here is a guide to help you get started:
1. Create a Jinja Template
A Jinja template is essentially a text file with special tags that will be replaced by dynamic values. For example, suppose you have a file template.txt
with the following content:
Hello {{ name }},
Thank you for your order of {{ product }}. Your order will be shipped to the following address:
{{ address }}.
Regards,
The Sales Team
2. Declare the Template
Next, declare the template in your playbook as follows:
- source: template.txt
destination: file.txt
template: jinja
compatibility:
- system:
template_data:
name: My name
product: My product
address: My address
The result after generating the package:
Hello My name,
Thank you for your order of My product. Your order will be shipped to the following address:
My address.
Regards,
The Sales Team
3. Use Control Structures
Jinja allows the use of control structures such as loops and conditions to generate more complex dynamic content. For example:
Hello {{ name }},
Thank you for your order of {{ product }}. Your order contains the following items:
{% for item in items %}
- {{ item }}
{% endfor %}
Your order will be shipped to the following address:
{{ address }}.
Regards,
The Sales Team
4. Declare the Template
And in the declaration:
- source: template.txt
destination: file.txt
template: jinja
compatibility:
- system:
template_data:
name: My name
product: My product
items: [Item1, Item2]
address: My address
The result after generating the package:
Hello My name,
Thank you for your order of My product. Your order contains the following items:
- Item1
- Item2
Your order will be shipped to the following address:
My address.
Regards,
The Sales Team
Go Further
Jinja is a very powerful template engine and offers advanced features that we won't cover here. To learn more, check out the official documentation (opens in a new tab). For a list of predefined functions in Jinja, see this documentation (opens in a new tab).
Know more about the template generation: How to declare a template
Finally, AIOP predefine variables for templates that you can find here.