Rendering Data with Jinja¶
🌐 AI ReaderRead this document in English or ask questions: Open in Claude, ChatGPT, or Gemini ↗
The data lives in Python, the document holds only the template. Change the data, rebuild, and every table follows.
Where the data comes from¶
conf.py registers cookiecutter_pywf_kb_site_demo.docs.doc_data as the jinja context doc_data:
# ``{jinja} doc_data`` renders its body with these variables, then parses the
# result in the page's own syntax (MyST in .md, reST in .rst).
from cookiecutter_pywf_kb_site_demo.docs import doc_data
jinja_contexts = {
"doc_data": {
"doc_data": doc_data,
},
}
The data itself is a list of tuples:
users = [
(1, "alice"),
(2, "bob"),
]
The {jinja} directive first renders its body with jinja, then parses the result in the page’s own syntax: MyST in a .md page, reST in a .rst page.
Example 1: Markdown table¶
Template:
```{jinja} doc_data
| id | name |
| :--- | :--- |
{% for id, name in doc_data.users -%}
| {{ id }} | {{ name }} |
{% endfor %}
```
Result:
id |
name |
|---|---|
1 |
alice |
2 |
bob |
Example 2: reST list-table¶
To emit reST from a .md page, wrap the rendered text in {eval-rst}. In a .rst page, write .. jinja:: doc_data directly, without eval-rst.
Template:
````{jinja} doc_data
```{eval-rst}
.. list-table:: Users
:header-rows: 1
:widths: 10 90
* - id
- name
{% for id, name in doc_data.users %}
* - {{ id }}
- {{ name }}
{% endfor %}
```
````
Result:
id |
name |
|---|---|
1 |
alice |
2 |
bob |
Example 3: list-table without reST¶
MyST has its own {list-table} directive, with the reST syntax inside a Markdown fence:
id |
name |
|---|---|
1 |
alice |
2 |
bob |