2020-09-20 11:24:46 +02:00
|
|
|
---
|
|
|
|
template: overrides/main.html
|
|
|
|
---
|
|
|
|
|
|
|
|
# Variables
|
|
|
|
|
|
|
|
Macros and variables are powerful tools to parametrize Markdown files, as they
|
|
|
|
allow to perform Jinja templating directly from Markdown. This is especially
|
|
|
|
useful to include technical data from other files and add central variables via
|
|
|
|
`mkdocs.yml`.
|
|
|
|
|
|
|
|
## Configuration
|
|
|
|
|
|
|
|
### Macros
|
|
|
|
|
|
|
|
The [macros][1] plugin adds support to reference variables and call macros and
|
|
|
|
supports Jinja templating directly from Markdown. It can be installed with
|
|
|
|
`pip`:
|
|
|
|
|
|
|
|
```
|
|
|
|
pip install mkdocs-macros-plugin
|
|
|
|
```
|
|
|
|
|
|
|
|
Then, add the following to `mkdocs.yml`:
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
plugins:
|
|
|
|
- macros
|
|
|
|
```
|
|
|
|
|
|
|
|
[1]: https://github.com/fralau/mkdocs_macros_plugin
|
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
### Using predefined variables
|
|
|
|
|
|
|
|
A set of predefined variables is enabled by default and can be used from
|
|
|
|
Markdown, including data from `mkdocs.yml`. More specifically, predefined
|
|
|
|
variables fall into the following categories:
|
|
|
|
|
|
|
|
- `config.*`: configuration parameters from `mkdocs.yml`
|
|
|
|
- `page.*`: metadata and content of current page
|
|
|
|
- `navigation.*`: list of all pages and sections
|
|
|
|
- `environment.*`: underlying operating system
|
|
|
|
- `git.*`: git-related information, if available
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
``` markdown
|
|
|
|
Welcome to {{ config.site_name }}!
|
|
|
|
```
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
``` markdown
|
|
|
|
Welcome to Material for MkDocs!
|
|
|
|
```
|
|
|
|
|
|
|
|
A list of all predefined variables can be printed with:
|
|
|
|
|
|
|
|
```
|
|
|
|
{{ macros_info() }}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Using custom variables
|
|
|
|
|
|
|
|
All data defined under `extra` in `mkdocs.yml` is automatically exposed as a
|
|
|
|
variable and can be used from the template. This enables centralized parameter
|
|
|
|
storage and management.
|
|
|
|
|
|
|
|
_Example_:
|
|
|
|
|
|
|
|
=== "docs/page.md"
|
|
|
|
|
|
|
|
```` markdown
|
|
|
|
The unit price is {{ unit.price }}
|
|
|
|
````
|
|
|
|
|
|
|
|
=== "mkdocs.yml"
|
|
|
|
|
|
|
|
``` yaml
|
|
|
|
extra:
|
|
|
|
unit:
|
|
|
|
price: 12.50
|
|
|
|
```
|
|
|
|
|
|
|
|
_Result_:
|
|
|
|
|
|
|
|
The unit price is 12.50.
|
|
|
|
|
2020-10-31 15:11:56 +01:00
|
|
|
### Using variables in snippets
|
|
|
|
|
2020-11-15 22:31:10 +01:00
|
|
|
The [macros][2] plugin can be used to allow variables in snippets, which is not
|
|
|
|
possible with the [Snippets][3] extension alone. Add the snippets location to
|
|
|
|
the plugin configuration in `mkdocs.yml`:
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
``` yaml
|
2020-10-31 15:11:56 +01:00
|
|
|
plugins:
|
2020-10-31 15:29:05 +01:00
|
|
|
- search
|
|
|
|
- macros:
|
|
|
|
include_dir: snippets
|
2020-10-31 15:11:56 +01:00
|
|
|
```
|
|
|
|
|
2020-11-07 18:01:46 +01:00
|
|
|
In your Markdown file, include snippets with Jinja's [`include`][4] function:
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
``` markdown
|
|
|
|
{% include "definitions.md" %}
|
2020-10-31 15:11:56 +01:00
|
|
|
```
|
|
|
|
|
2020-11-07 18:01:46 +01:00
|
|
|
_Example_:
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
=== "snippets/definitions.md"
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
``` markdown
|
|
|
|
The unit price is {{ page.meta.unit.price }}
|
2020-10-31 15:11:56 +01:00
|
|
|
```
|
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
=== "docs/page-1.md"
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
``` markdown
|
2020-10-31 15:11:56 +01:00
|
|
|
---
|
2020-10-31 15:29:05 +01:00
|
|
|
unit:
|
|
|
|
price: 12.50
|
2020-10-31 15:11:56 +01:00
|
|
|
---
|
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
{% include "definitions.md" %}
|
2020-10-31 15:11:56 +01:00
|
|
|
```
|
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
=== "docs/page-2.md"
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
``` markdown
|
2020-10-31 15:11:56 +01:00
|
|
|
---
|
2020-10-31 15:29:05 +01:00
|
|
|
unit:
|
|
|
|
price: 25.00
|
2020-10-31 15:11:56 +01:00
|
|
|
---
|
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
{% include "definitions.md" %}
|
2020-10-31 15:11:56 +01:00
|
|
|
```
|
|
|
|
|
2020-11-15 22:31:10 +01:00
|
|
|
[2]: #macros
|
|
|
|
[3]: https://facelessuser.github.io/pymdown-extensions/extensions/snippets/
|
2020-10-31 15:29:05 +01:00
|
|
|
[4]: https://jinja.palletsprojects.com/en/2.11.x/templates/#include
|
2020-10-31 15:11:56 +01:00
|
|
|
|
2020-09-20 11:24:46 +02:00
|
|
|
## Customization
|
|
|
|
|
|
|
|
### Custom macros
|
|
|
|
|
2020-09-20 11:30:20 +02:00
|
|
|
The [macros][1] plugin allows to define custom macros, which can then be used
|
2020-10-31 15:29:05 +01:00
|
|
|
from Markdown files. See the [official documentation][5] for more information
|
2020-09-20 11:24:46 +02:00
|
|
|
how to define custom macros.
|
|
|
|
|
2020-10-31 15:29:05 +01:00
|
|
|
[5]: https://mkdocs-macros-plugin.readthedocs.io/en/latest/python/
|