Mako template inheritance, defs and HTML escaping: working versions (tested 2026-10-16)
Load Mako templates from an in-memory TemplateLookup, inherit a base layout, call defs and apply filters with safe HTML escaping by default.
passed5 passed · 0 failed2026-10-16 21:57 UTC- Status
- passed
- Last passed
- 2026-10-16 21:57 UTC
- Tests
- 5 / 5
- Language
- Python
- Runtime
- Python 3.13.16
pinned versions2
mako@1.4.3pytest@9.1.1
run it
uv venv -q .venv && uv pip install -q --python .venv/bin/python -r requirements.txt .venv/bin/python -m pytest -q
test environment
Python 3.13.16 linux 6.17.0-1022-azure x64 runner: github-actions 38972656245
The code
Exactly the files that passed. Your agent can pull them with get_recipe("mako-template-inheritance") (install), or see them on GitHub.
requirements.txt
mako==1.4.3 pytest==9.1.1
renderer.py
from mako.lookup import TemplateLookup
TEMPLATES = {
"base.html": """\
<!doctype html>
<html>
<head><title>${self.page_title()}</title></head>
<body>
${self.body()}
</body>
</html>
""",
"page.html": """\
<%inherit file="base.html"/>
<%!
def shout(text):
return text.upper() + "!"
%>
<%def name="page_title()">${title}</%def>
<%def name="tag(name)"><span class="tag">${name}</span></%def>
<h1>${title}</h1>
<p>${subtitle | trim}</p>
<ul>
% for item in items:
<li>${item}</li>
% endfor
</ul>
<footer>${shout("bye")}</footer>
${self.tag("featured")}
""",
}
def build_lookup(**options):
lookup = TemplateLookup(default_filters=["h"], **options)
for uri, source in TEMPLATES.items():
lookup.put_string(uri, source)
return lookup
def render_page(title, subtitle, items):
template = build_lookup().get_template("page.html")
return template.render(title=title, subtitle=subtitle, items=items)
tests/test_renderer.py
import pytest
from renderer import build_lookup, render_page
def test_inherits_base_and_renders_def():
html = render_page("My <Page>", " hello ", ["a"])
assert html.startswith("<!doctype html>")
assert "<title>My <Page></title>" in html
assert "<h1>My <Page></h1>" in html
def test_escapes_loop_items_by_default():
html = render_page("t", "s", ["<script>alert(1)</script>", "ok"])
assert "<li><script>alert(1)</script></li>" in html
assert "<script>" not in html
def test_inherited_body_is_not_double_escaped():
html = render_page("t", "s", [])
assert '<span class="tag">featured</span>' in html
assert "<span" not in html
def test_trim_and_custom_filter():
html = render_page("t", " spaced ", [])
assert "<p>spaced</p>" in html
assert "<footer>BYE!</footer>" in html
def test_undefined_variable_raises():
template = build_lookup().get_template("page.html")
with pytest.raises(NameError):
template.render(subtitle="s", items=[])
pytest.ini
[pytest] pythonpath = .
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.