You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/howto/use_jinja2.md
+44-4Lines changed: 44 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Using jinja2
1
+
# Use jinja2 for automation
2
2
3
3
`table-reader` supports [`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/), which enables you to use jinja2 syntax inside markdown files (among other things).
4
4
@@ -10,8 +10,9 @@ plugins:
10
10
- table-reader
11
11
```
12
12
13
-
Now you can do cool things like dynamically load a list of tables:
13
+
Now you can do cool things like:
14
14
15
+
## Dynamically load a list of tables
15
16
16
17
```markdown
17
18
# index.md
@@ -22,10 +23,9 @@ Now you can do cool things like dynamically load a list of tables:
22
23
{ { read_csv(table_name) }}
23
24
24
25
{% endfor %}
25
-
26
26
```
27
27
28
-
## Indented content like content tabs
28
+
## Insert tables into content tabs
29
29
30
30
If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like [content tabs](https://squidfunk.github.io/mkdocs-material/reference/content-tabs/#usage) will not work as expected.
31
31
@@ -69,3 +69,43 @@ To fix that, you can use the custom _filter_ `add_indendation` (a filter add to
69
69
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
70
70
If you copy this example, make sure to fix.
71
71
72
+
73
+
## Recursively insert an entire directory of tables
74
+
75
+
[`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/) enables you to define additional functions (called _macros_) that you will be able to use within your markdown files.
76
+
See their documentation on how to set this up. Here's an example with some functions to interact with the filesystem:
77
+
78
+
```python
79
+
defdefine_env(env):
80
+
"""
81
+
Register additional mkdocs-macros-plugin functions that can be used as macros in markdown files.
82
+
"""
83
+
@env.macro
84
+
deflistdir(path):
85
+
return os.listdir(path)
86
+
87
+
@env.macro
88
+
defpath_exists(path):
89
+
return Path(path).exists()
90
+
91
+
@env.macro
92
+
defis_file(path):
93
+
return Path(path).is_file()
94
+
```
95
+
96
+
Now you could do something like:
97
+
98
+
```markdown
99
+
# index.md
100
+
101
+
{% for table_name in listdir('docs/assets/my_tables") %}
102
+
103
+
{ { read_csv(table_name) }}
104
+
105
+
{% endfor %}
106
+
```
107
+
108
+
!!! note "Note the space in { {"
109
+
110
+
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
0 commit comments