Skip to content

Commit

Permalink
Add TILs about jinja2
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourai committed Apr 22, 2024
1 parent 6abbd50 commit 91cb13c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions python/list-of-dictionaries-as-jinja2-variables.md
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%}
```
10 changes: 10 additions & 0 deletions python/read-jinja2-template-from-file.md
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>))
```

0 comments on commit 91cb13c

Please sign in to comment.