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
class PageTemplate
A jinja template used to build a website from an MkPage.
mknodes.pages.templateblocks
AnalyticsBlock
class AnalyticsBlock(Block)
Block for analytics-tags in HEAD.
BaseBlock
class BaseBlock(mknode.MkNode)
A class representing a block from a page template.
to_markdown
def to_markdown(flavor: str | rendering.Renderer | None = None, *, md: markdown.Markdown | None = None) -> str
Return the Jinja block (with HTML content) for the page template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flavor | str | rendering.Renderer | None | Unused: template blocks are theme overrides, not Markdown | None |
md | markdown.Markdown | None | Markdown parser to use | None |
BaseJSBlock
class BaseJSBlock(BaseBlock)
Block for additional libraries.
__init__
def __init__(scripts: list[resources.JSFile] | None = None, include_super: bool = True) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scripts | list[resources.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
def add_script_file(script: resources.JSFile | str | os.PathLike[str]) -> None
Add a script file to the block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
script | resources.JSFile | str | os.PathLike[str] | Script to add to the block |
Block
class Block(BaseBlock)
Base class for head blocks.
ExtraHeadBlock
class ExtraHeadBlock(Block)
Block for extra HEAD content.
Can be used for stuff not covered by other blocks, like changing the indexing rules.
set_redirect_url
def 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 |
set_robots_rule
def 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
class HtmlBlock(BaseBlock)
Base class for blocks which usually contain HTML content.
__init__
def __init__(block_id: str, *, parent: mk.MkPage | mk.MkNav | None = None) -> None
Constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_id | str | Name of the block | |
parent | mk.MkPage | mk.MkNav | None | Parent node | None |
block_content
def block_content(md: markdown.Markdown | None = None) -> str
Return the actual block content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
md | markdown.Markdown | None | Markdown parser to use | None |
LibsBlock
class LibsBlock(BaseJSBlock)
Block for additional libraries.
ScriptsBlock
class ScriptsBlock(BaseJSBlock)
Block for JavaScripts at end of BODY.
SiteMetaBlock
class SiteMetaBlock(Block)
Block for meta tags in HEAD.
StylesBlock
class StylesBlock(BaseBlock)
Block for additional stylesheets.
add_css
def 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 |
Super
class Super
Simple class to use for the jinja super expression.
TitleBlock
class TitleBlock(BaseBlock)
Block wrapping the HTML title.
Page info
Code for this page
mknodes.manual.page_section._
@nav.route.page("Templates", hide="toc", status="new")
def _(page: mk.MkPage) -> None:
page += mk.MkTemplate("pages/page_templates.jinja")
assert page.template is not None
page.template.announce.content = mk.MkMetadataBadges(typ="classifiers")
page.template.footer.content = mk.MkProgressBar(50)
code = "information = 'You can even put MkNodes here!'"
page.template.tabs.content = mk.MkCode(f"{code}")
page.template.hero.content = mk.MkHeader("A header!")
css = {
":root": {
"--md-primary-fg-color": "#FF0000",
"--md-primary-fg-color--light": "#FF0000",
"--md-primary-fg-color--dark": "#FF0000",
},
}
page.template.styles.add_css(css)
Resources
Resources(css=[],
markdown_extensions={},
plugins=[Plugin('mkdocstrings')],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/page_section.py
source_function: _
source_line_no: 54
hide:
- toc
status: new
title: Templates