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 thedatadirectory.WEBROOT: The full path to thehtdocsdirectory.CONTENTDIR: The full path to thecontentdirectory.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: AnMDContentListrepresenting 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_URLvalue for this content – see below). Note thatMDCONTENTis not available inside shortcodes. AnMDContentListis a list object with some convenience methods for filtering and sorting. It will be described further later on.- Whatever is defined under
template_contextin thewmk_config.yamlfile (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 thefingerprinttemplate filter.LOADER: The template loader/env. In the case of Mako, this is aTemplateLookupobject; in the case of Jinja2 this is anEnvironmentobject with aFileSystemLoaderloader.site: A dict-like object containing the variables specified under thesitekey 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 MakoTemplateLookupinstance 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_moduleand 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_datefound in the YAML front matter, or theMTIMEvalue 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.yamlfiles in the markdown file directory and its parent directories insidecontent, and possibly in YAML files from thedatadirectory loaded via theLOADdirective 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.