mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Added documentation on using variables in snippets (#2006)
* variables do not work in snippets included using Snippets extension * workaround: use macros plugin to define snippets (as described here https://mkdocs-macros-plugin.readthedocs.io/en/latest/advanced/#including-snippets-in-pages)
This commit is contained in:
parent
68c04f8298
commit
79e06cac0b
@ -88,6 +88,70 @@ _Result_:
|
|||||||
|
|
||||||
The unit price is 12.50.
|
The unit price is 12.50.
|
||||||
|
|
||||||
|
### Using variables in snippets
|
||||||
|
|
||||||
|
You may want to use variables in snippets, for example, when describing the same procedure in multiple contexts where only one piece of information differs. This does not work with snippets that are included using the [Snippets](../abbreviations.md#snippets) extension. Instead, you can use the [macros](#macros) plugin for defining snippets.
|
||||||
|
|
||||||
|
To this end, add the snippet location using the `include_dir` parameter to the plugin's configuration in `mkdocs.yml`, for example:
|
||||||
|
|
||||||
|
```
|
||||||
|
plugins:
|
||||||
|
- search
|
||||||
|
- macros:
|
||||||
|
include_dir: snippets
|
||||||
|
```
|
||||||
|
|
||||||
|
In your Markdown file, you call the snippets like
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
{% include 'snip.md' %}
|
||||||
|
```
|
||||||
|
|
||||||
|
This example illustrates the behavior:
|
||||||
|
|
||||||
|
=== "snippets/snip.md"
|
||||||
|
|
||||||
|
_Code_
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
Here goes the variable: {{ page.meta.variables.number }}
|
||||||
|
```
|
||||||
|
|
||||||
|
=== "docs/page1.md"
|
||||||
|
|
||||||
|
_Code_
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
variables:
|
||||||
|
number: 500
|
||||||
|
---
|
||||||
|
|
||||||
|
{% include 'snip.md' %}
|
||||||
|
```
|
||||||
|
|
||||||
|
_Result_
|
||||||
|
|
||||||
|
Here goes the variable: 500
|
||||||
|
|
||||||
|
=== "docs/page2.md"
|
||||||
|
|
||||||
|
_Code_
|
||||||
|
|
||||||
|
```markdown
|
||||||
|
---
|
||||||
|
variables:
|
||||||
|
number: 1000
|
||||||
|
---
|
||||||
|
|
||||||
|
{% include 'snip.md' %}
|
||||||
|
```
|
||||||
|
|
||||||
|
_Result_
|
||||||
|
|
||||||
|
Here goes the variable: 1000
|
||||||
|
|
||||||
|
|
||||||
## Customization
|
## Customization
|
||||||
|
|
||||||
### Custom macros
|
### Custom macros
|
||||||
|
Loading…
Reference in New Issue
Block a user