Templates
The page template can be edited easily via the MkPage.template attribute. If any block gets modified then MkNodes will generate an individual template HTML file for the page during the build process.
An example:
would generate the following template override automatically:{ % extends "main.html" % }
{ % block content % }
<div class="admonition info">
<p class="admonition-title">Info</p>
<p>Some text</p>
</div>
{ % endblock % }
page.template.announce.content = mk.MkMetadataBadges(typ="classifiers")
page.template.footer.content = mk.MkProgressBar(50)
page.template.tabs.content = mk.MkCode("information = 'You can even put MkNodes here!'")
page.template.hero.content = mk.MkHeader("A header!")
PageTemplate Blocks¶
Title block (MkPage.template.title)¶
The title block allows to modify the title tag in HEAD
Libs block (MkPage.template.libs)¶
The libs block can be used to add additional JavaScript libraries.
Analytics block (MkPage.template.analytics)¶
The analytics block can be used to set custom analytics.
Scripts block (MkPage.template.scripts)¶
The scripts block can be used to add custom JavaScript files, put at the end of the HTML body.
Site meta block (MkPage.template.site_meta)¶
The site_meta block contains the site metadata. By default it gets populated by data from the MkDocs config file.
Styles block (MkPage.template.styles)¶
The styles block can be used to add additional CSS.
ExtraHead block (MkPage.template.styles)¶
The extrahead block allows to add content to the HTML head which does not suit the other categories, like redirecting to another URL.
HTML blocks¶
HTML blocks can get populated with Markdown, which will get converted to HTML during the build process.
MkDocs-Material-exclusive blocks:
page.template.announce.content = mk.MkMetadataBadges(typ="classifiers")
page.template.footer.content = mk.MkProgressBar(50)
page.template.tabs.content = mk.MkCode("information = 'You can even put MkNodes here!'")
page.template.hero.content = mk.MkHeader("A header!")
API¶
mknodes.PageTemplate
¶
A jinja template used to build a website from an MkPage.
mknodes.pages.templateblocks
¶
BaseBlock
¶
Bases: MkNode
A class representing a block from a page template.
to_markdown
¶
to_markdown(flavor: str | Renderer | None = None, *, md: Markdown | None = None) -> str
Return the Jinja block (with HTML content) for the page template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flavor
|
str | Renderer | None
|
Unused: template blocks are theme overrides, not Markdown |
None
|
md
|
Markdown | None
|
Markdown parser to use |
None
|
BaseJSBlock
¶
Bases: BaseBlock
Block for additional libraries.
__init__
¶
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scripts
|
list[JSFile] | None
|
List of scripts to add to HTML header libs. |
None
|
include_super
|
bool
|
Whether to include the original content. |
True
|
add_script_file
¶
Add a script file to the block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script
|
JSFile | str | PathLike[str]
|
Script to add to the block |
required |
ExtraHeadBlock
¶
Bases: Block
Block for extra HEAD content.
Can be used for stuff not covered by other blocks, like changing the indexing rules.
set_redirect_url
¶
Set a URL which the page should redirect to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
URL to redirect to |
required |
set_robots_rule
¶
Set a rule for search robots.
For valid rule values, check https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
str | None
|
Rule to apply for robots |
None
|
HtmlBlock
¶
LibsBlock
¶
ScriptsBlock
¶
StylesBlock
¶
Super
¶
Simple class to use for the jinja super expression.