Context variables
The Mako/Jinja2 templates, whether they are stand-alone or being used to render markdown (or other) content, receive the following context variables:
DATADIR
: The full path to thedata
directory.WEBROOT
: The full path to thehtdocs
directory.CONTENTDIR
: The full path to thecontent
directory.TEMPLATES
: A list of all templates which will potentially be rendered as stand-alone. Each item in the list contains the keyssrc
(relative path to the source template),src_path
(full path to the source template),target
(full path of the file to be written), andurl
(relative url to the file to be written).MDCONTENT
: AnMDContentList
representing all the content files which will potentially be rendered by a template. Each item in the list contains the keyssource_file
,source_file_short
(truncated and full paths to the source),target
(html file to be written),template
(filename of the template which will be used for rendering),data
(most of the context variables seen by this content),doc
(the raw content document source), andurl
(theSELF_URL
value for this content – see below). Note thatMDCONTENT
is not available inside shortcodes. AnMDContentList
is a list object with some convenience methods for filtering and sorting. It will be described further later on.- Whatever is defined under
template_context
in thewmk_config.yaml
file (see Configuration file). SELF_URL
: The relative path to the HTML file which the output of the template will be written to.SELF_TEMPLATE
: The path to the current template file (from the template root).ASSETS_MAP
: A map of fingerprinted assets (such as javascript or css files), used by thefingerprint
template filter.LOADER
: The template loader/env. In the case of Mako, this is aTemplateLookup
object; in the case of Jinja2 this is anEnvironment
object with aFileSystemLoader
loader.site
: A dict-like object containing the variables specified under thesite
key inwmk_config.yaml
.CACHE
: An ordinary dictionary object, intended for use by templates as a simple shared in-memory cache.
In the case of Jinja2 templates, three extra context variables are available:
mako_lookup
: A MakoTemplateLookup
instance which makes it possible to call Mako templates from a Jinja2 template.get_context
: A function returning all context variables as a dict.import
: An alias forimportlib.import_module
and can thus be used to import a Python module into a Jinja template as the value of a variable, e.g.{% set utils = import('my_utils') %}
. The main intent is to make code inside the projectpy/
subdirectory as easily available in Jinja templates as it is in Mako templates.
When templates are rendering markdown (or other) content, they additionally get the following context variables:
CONTENT
: The rendered HTML produced from the source document.RAW_CONTENT
: The original source document.SELF_FULL_PATH
: The full filesystem path to the source document file.SELF_SHORT_PATH
: The path to the source document file relative to the content directory.MTIME
: A datetime object representing the modification time for the source file.DATE
: A datetime object representing the first found value ofdate
,pubdate
,modified_date
,expire_date
, orcreated_date
found in the YAML front matter, or theMTIME
value as a fallback. Since this is guaranteed to be present, it is natural to use it for sorting and generic display purposes.RENDERER
: A callable which enables a template to render markdown inwmk
's own environment. This is mainly so that it is possible to support shortcodes which depend on other markdown content which itself may contain shortcodes. The callable receives a dict containing the keysdoc
(the markdown) anddata
(the context variables) and returns rendered HTML.page
: A dict-like object containing the variables defined in the YAML meta section at the top of the markdown file, inindex.yaml
files in the markdown file directory and its parent directories insidecontent
, and possibly in YAML files from thedata
directory loaded via theLOAD
directive in the metadata.
For further details on context variables set in the document frontmatter and in
index.yaml
files, see Site, page and nav variables.