Unleash the power of MkDocs with macros and variables
—
Predefined variables and context objects automatically available in templates, including environment information, git data, page metadata, and site configuration.
Information about the system environment, versions, and runtime context.
environment: dict = {
'system': str, # Operating system name ('Linux', 'Windows', 'MacOs')
'system_version': str, # OS version string
'python_version': str, # Python version string
'mkdocs_version': str, # MkDocs version
'macros_plugin_version': str, # Plugin version
'jinja2_version': str # Jinja2 version
}System-specific content:
Running on {{ environment.system }} {{ environment.system_version }}
Built with Python {{ environment.python_version }}
{% if environment.system == 'Windows' %}
Use `copy` command instead of `cp`.
{% else %}
Use standard Unix commands.
{% endif %}Version information:
Documentation built with:
- MkDocs {{ environment.mkdocs_version }}
- MkDocs Macros {{ environment.macros_plugin_version }}
- Python {{ environment.python_version }}Access to the plugin's configuration from mkdocs.yml.
plugin: dict # Complete plugin configuration from mkdocs.yml macros sectionConfiguration-based content:
{% if plugin.verbose %}
Debug mode is enabled.
{% endif %}
Module name: {{ plugin.module_name }}
{% if plugin.include_dir %}
Templates loaded from: {{ plugin.include_dir }}
{% endif %}Git repository metadata and commit information.
git: dict = {
'status': bool, # True if git information was successfully retrieved
'commit': str, # Full commit hash
'short_commit': str, # Abbreviated commit hash
'tag': str, # Current git tag (if any)
'short_tag': str, # Tag without version suffix
'author': str, # Last commit author name
'author_email': str, # Last commit author email
'committer': str, # Last commit committer name
'committer_email': str, # Last commit committer email
'date': datetime, # Last commit date as datetime object
'date_ISO': str, # Last commit date as ISO string
'message': str, # Last commit message
'raw': str, # Raw git log output
'root_dir': str # Git repository root directory
}Commit information:
Last updated: {{ git.date.strftime('%Y-%m-%d') }}
Commit: {{ git.short_commit }}
{% if git.tag %}
Version: {{ git.short_tag }}
{% endif %}
{% if git.status %}
Repository: {{ git.root_dir }}
Author: {{ git.author }}
{% else %}
Not a git repository
{% endif %}Build metadata:
---
Built from commit [{{ git.short_commit }}]({{ config.repo_url }}/commit/{{ git.commit }})
on {{ git.date.strftime('%B %d, %Y') }}
---Complete MkDocs configuration with all settings and metadata.
config: dict # Complete mkdocs.yml configurationSite information:
# {{ config.site_name }}
{{ config.site_description }}
{% if config.site_url %}
Visit us at: {{ config.site_url }}
{% endif %}
{% if config.repo_url %}
Source code: [{{ config.repo_name }}]({{ config.repo_url }})
{% endif %}Theme and plugin configuration:
{% if config.theme.name == 'material' %}
Using Material theme with features:
{% for feature in config.theme.features %}
- {{ feature }}
{% endfor %}
{% endif %}
Active plugins:
{% for plugin in config.plugins %}
- {{ plugin }}
{% endfor %}Variables defined in the extra section of mkdocs.yml.
extra: dict # Variables from mkdocs.yml extra section
# Also available as top-level variablesmkdocs.yml:
extra:
version: '2.1.0'
author: 'John Doe'
social:
twitter: '@johndoe'
github: 'johndoe'Template usage:
Version: {{ version }} <!-- or {{ extra.version }} -->
Author: {{ author }}
Follow us:
- [Twitter](https://twitter.com/{{ social.twitter }})
- [GitHub](https://github.com/{{ social.github }})Current page metadata and properties (available during page rendering).
page: Page = {
'title': str, # Page title
'url': str, # Page URL
'abs_url': str, # Absolute page URL
'canonical_url': str, # Canonical URL
'edit_url': str, # Edit URL (if configured)
'file': File, # File object with path information
'content': str, # Rendered page content (HTML)
'toc': list, # Table of contents
'meta': dict, # Page metadata from YAML front matter
'previous_page': Page, # Previous page in navigation
'next_page': Page # Next page in navigation
}Page metadata:
# {{ page.title }}
{% if page.meta.description %}
{{ page.meta.description }}
{% endif %}
File: `{{ page.file.src_path }}`
{% if page.edit_url %}
[Edit this page]({{ page.edit_url }})
{% endif %}Navigation:
{% if page.previous_page %}
← [{{ page.previous_page.title }}]({{ page.previous_page.url }})
{% endif %}
{% if page.next_page %}
[{{ page.next_page.title }}]({{ page.next_page.url }}) →
{% endif %}Page-specific logic:
{% if page.file.src_path.startswith('api/') %}
This is an API documentation page.
{% endif %}
{% if 'draft' in page.meta %}
⚠️ This page is a draft
{% endif %}Complete site navigation structure with all pages and sections.
navigation: Navigation # Site navigation object with pages and hierarchyNavigation menu:
## Site Contents
{% for item in navigation.items %}
{% if item.is_page %}
- [{{ item.title }}]({{ item.url }})
{% elif item.is_section %}
- {{ item.title }}
{% for child in item.children %}
- [{{ child.title }}]({{ child.url }})
{% endfor %}
{% endif %}
{% endfor %}Page count:
This site contains {{ navigation.pages | length }} pages.Collection of all files in the site.
files: Files # Collection of all site filesFile listing:
## All Files
{% for file in files %}
- {{ file.src_path }} → {{ file.dest_path }}
{% endfor %}Conditional includes:
{% for file in files %}
{% if file.name == 'changelog.md' %}
{% include file.src_path %}
{% endif %}
{% endfor %}Variables are resolved in this order (later overrides earlier):
{% set %} statementsAll built-in variables support nested access:
<!-- Dot notation -->
{{ config.theme.name }}
{{ git.date.year }}
{{ page.file.src_path }}
<!-- Bracket notation -->
{{ config['site_name'] }}
{{ environment['python_version'] }}
<!-- With defaults -->
{{ config.repo_name | default('Unknown Repository') }}Install with Tessl CLI
npx tessl i tessl/pypi-mkdocs-macros-plugin