-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Using a list of dictionaries as variables to render a jinja2 template | ||
|
||
```python | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
environment = Environment(loader=FileSystemLoader("./")) | ||
template = environment.get_template("template.j2") | ||
|
||
list_of_dicts = [{"a":1, "b":2}, {"a":3, "b":4}] | ||
|
||
print(template.render(variables=list_of_dicts)) | ||
``` | ||
|
||
The important part is the `variables` kwarg. | ||
|
||
```jinja2 | ||
{% for var in variables %} | ||
{{ var.a }} | ||
{{ var.b }} | ||
{% endfor%} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# How to open an jinja2 template from a file | ||
|
||
```python | ||
from jinja2 import Environment, FileSystemLoader | ||
|
||
environment = Environment(loader=FileSystemLoader("./")) | ||
template = environment.get_template("template.j2") | ||
|
||
print(template.render(<variables>)) | ||
``` |