Skip to content

Commit 91cb13c

Browse files
committed
Add TILs about jinja2
1 parent 6abbd50 commit 91cb13c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Using a list of dictionaries as variables to render a jinja2 template
2+
3+
```python
4+
from jinja2 import Environment, FileSystemLoader
5+
6+
environment = Environment(loader=FileSystemLoader("./"))
7+
template = environment.get_template("template.j2")
8+
9+
list_of_dicts = [{"a":1, "b":2}, {"a":3, "b":4}]
10+
11+
print(template.render(variables=list_of_dicts))
12+
```
13+
14+
The important part is the `variables` kwarg.
15+
16+
```jinja2
17+
{% for var in variables %}
18+
{{ var.a }}
19+
{{ var.b }}
20+
{% endfor%}
21+
```
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# How to open an jinja2 template from a file
2+
3+
```python
4+
from jinja2 import Environment, FileSystemLoader
5+
6+
environment = Environment(loader=FileSystemLoader("./"))
7+
template = environment.get_template("template.j2")
8+
9+
print(template.render(<variables>))
10+
```

0 commit comments

Comments
 (0)