Boni García
Boni García

Reputation: 4858

Global variables in MkDocs

I have a documentation project made with MkDocs. I would like to define global variables in the configuration file (mkdocs.yml) to be used in the markdown pages (*.md).

Reading this issue, it seems it can be done by including an extra configuration in the mkdocs.yml file, for example:

extra:
    version: 1.0

... and then, use that variable in page for example as follows:

---> My version: {{ config.extra.version }}

I tried that, but unfortunately it is not working in my example (no variable substution):

enter image description here

Upvotes: 15

Views: 7128

Answers (3)

n2ygk
n2ygk

Reputation: 489

The given answer is out of date as this can be done with plugins like macros or markdownextradata as mentioned above except you would just reference {{ version }}.

Upvotes: 9

fralau
fralau

Reputation: 3819

As an update, it is possible to insert the variables from the extra slot in mkocs.yml, exactly as you describe.

To make this work, you need to install the markdownextradata plugin.

Upvotes: 3

Waylan
Waylan

Reputation: 42497

No, this is not possible at this time.

You say that you "use that variable in page". I'm assuming you mean a "Markdown" page. At this time template variables are not available in the Markdown pages. The template engine is not even run against the Markdown. The output of the Markdown parser is one of the variables passed to the template. For a more detailed explanation of how that works, see my answer to How do you include flask/jinja2 code inside a markdown file?.

Specific to MkDocs, there is an open issue (#304) discussing adding a feature to support some limited templating within the Markdown pages, but it is scheduled for post-1.0, so its not a top priority at this time.

Upvotes: 4

Related Questions