httk.serve.web.templating

Provide template engine contracts and built-in implementations.

Submodules

Classes

TemplateEngine

Define the page and fragment template-engine protocol.

TemplateRenderInput

Carry page content and context into a template engine.

HttkCompatTemplateEngine

Legacy-oriented template resolution for old httkweb projects.

JinjaTemplateEngine

Resolve and render Jinja templates from a site directory.

Package Contents

class httk.serve.web.templating.TemplateEngine[source]

Bases: Protocol

Define the page and fragment template-engine protocol.

render(render_input)[source]

Render page content through the configured templates.

render_fragment(*, template_name, context)[source]

Render one optional fragment template.

class httk.serve.web.templating.TemplateRenderInput[source]

Carry page content and context into a template engine.

Parameters:
  • content_html – Rendered page HTML supplied to the template.

  • template_name – Optional content template name.

  • base_template_name – Optional base template name.

  • context – Values exposed to templates.

content_html: str
template_name: str | None
base_template_name: str | None
context: dict[str, object]
class httk.serve.web.templating.HttkCompatTemplateEngine(template_dir)[source]

Bases: httk.serve.web.templating.jinja2_engine.JinjaTemplateEngine

Legacy-oriented template resolution for old httkweb projects.

The compatibility engine keeps Jinja rendering but prioritizes legacy template suffixes so old .httkweb.html files resolve first.

Parameters:

template_dir (pathlib.Path) – Directory containing site templates.

render(render_input)[source]

Render page content using legacy-compatible template resolution.

Parameters:

render_input (httk.serve.web.templating.base.TemplateRenderInput) – Content, template names, and template context.

Returns:

Rendered HTML.

Return type:

str

render_fragment(*, template_name, context)[source]

Render an optional fragment using legacy template conventions.

Parameters:
  • template_name (str) – Fragment template name.

  • context (dict[str, object]) – Values exposed to the fragment.

Returns:

Rendered fragment, or None when no template exists.

Return type:

str | None

class httk.serve.web.templating.JinjaTemplateEngine(template_dir, *, template_suffixes=TEMPLATE_SUFFIXES)[source]

Resolve and render Jinja templates from a site directory.

Parameters:
  • template_dir (pathlib.Path) – Directory containing site templates.

  • template_suffixes (tuple[str, Ellipsis]) – Suffixes probed for bare template names.

template_dir
template_suffixes = ('.html.j2', '.jinja', '.j2', '.html', '.httkweb.html')
render(render_input)[source]

Render page content through optional content and base templates.

Parameters:

render_input (httk.serve.web.templating.base.TemplateRenderInput) – Content, template names, and template context.

Returns:

Rendered HTML.

Return type:

str

render_fragment(*, template_name, context)[source]

Render an optional named fragment template.

Parameters:
  • template_name (str) – Fragment template name.

  • context (dict[str, object]) – Values exposed to the fragment.

Returns:

Rendered fragment, or None when no template exists.

Return type:

str | None