Skip to content

A header!

information = 'You can even put MkNodes here!'

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:

page.template.tabs.content = mk.MkAdmonition("Some text.")
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

page.template.title.content = "New title"

Libs block (MkPage.template.libs)

The libs block can be used to add additional JavaScript libraries.

page.template.libs.add_script_file("path_to_file.js")

Analytics block (MkPage.template.analytics)

The analytics block can be used to set custom analytics.

page.template.analytics.content = "<html content>"

Scripts block (MkPage.template.scripts)

The scripts block can be used to add custom JavaScript files, put at the end of the HTML body.

page.template.scripts.add_script_file("path_to_script.js")

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.

page.template.styles.add_css("My custom 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.

page.template.extra_head.set_redirect_url("http://some.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.

blocks property

blocks: list[BaseBlock]

A list containing all blocks of the PageTemplate.

__init__

__init__(
    filename: str | None = None,
    *,
    extends: str | None = "base.html",
    parent: MkPage | MkNav | None = None
) -> None

Constructor.

Parameters:

Name Type Description Default
filename str | None

Optional filename for the Template (can get overridden during build)

None
extends str | None

Parent template path

'base.html'
parent MkPage | MkNav | None

The MkPage using this Page template.

None

build_html

build_html(md: Markdown | None = None) -> str | None

Convert given PageTemplate to HTML.

Parameters:

Name Type Description Default
md Markdown | None

Markdown parser to use for HTML conversion.

None

mknodes.pages.templateblocks

AnalyticsBlock

Bases: Block

Block for analytics-tags in HEAD.

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__
__init__(scripts: list[JSFile] | None = None, include_super: bool = True) -> None

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_script_file(script: JSFile | str | PathLike[str]) -> None

Add a script file to the block.

Parameters:

Name Type Description Default
script JSFile | str | PathLike[str]

Script to add to the block

required

Block

Bases: BaseBlock

Base class for head blocks.

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_redirect_url(url: str) -> None

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_robots_rule(rule: str | None = None) -> None

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

Bases: BaseBlock

Base class for blocks which usually contain HTML content.

__init__
__init__(block_id: str, *, parent: MkPage | MkNav | None = None) -> None

Constructor.

Parameters:

Name Type Description Default
block_id str

Name of the block

required
parent MkPage | MkNav | None

Parent node

None
block_content
block_content(md: Markdown | None = None) -> str

Return the actual block content.

Parameters:

Name Type Description Default
md Markdown | None

Markdown parser to use

None

LibsBlock

Bases: BaseJSBlock

Block for additional libraries.

ScriptsBlock

Bases: BaseJSBlock

Block for JavaScripts at end of BODY.

SiteMetaBlock

Bases: Block

Block for meta tags in HEAD.

StylesBlock

Bases: BaseBlock

Block for additional stylesheets.

add_css
add_css(css: str | dict[str, Any]) -> None

Add CSS in form of a string or a CSS-rule like dictionary.

Parameters:

Name Type Description Default
css str | dict[str, Any]

A string or dict containing CSS

required

Super

Simple class to use for the jinja super expression.

TitleBlock

Bases: BaseBlock

Block wrapping the HTML title.

50%