Skip to main content

Module template

Module template 

Source
Expand description

§modo::template

MiniJinja-based template rendering for modo.

This module provides an opinionated template layer built on top of MiniJinja. It covers engine construction, per-request context injection, HTMX-aware rendering, i18n with plural rules, and cache-busted static-asset URLs.

§Provides

Type / TraitDescription
Engine / EngineBuilderCompile and cache templates from disk, register custom functions and filters, and override the locale resolver chain.
TemplateConfigConfiguration for template paths, static-asset prefix, locale defaults, and cookie/query-param names.
TemplateContextPer-request key-value map shared between middleware and handlers; handler values override middleware values on key conflicts.
TemplateContextLayerTower middleware that injects per-request data (current_url, is_htmx, request_id, locale, csrf_token, flash_messages, and tier_* entries) into every request’s extensions. Also re-exported as modo::middlewares::TemplateContext.
RendererAxum extractor that gives handlers a ready-to-use render handle.
HxRequestInfallible axum extractor that detects the HX-Request: true header. Also re-exported from modo::extractors.
contextRe-export of minijinja::context! for building template data in handlers.
LocaleResolverTrait for pluggable locale detection from a request.
QueryParamResolverResolves the active locale from a URL query parameter.
CookieResolverResolves the active locale from a cookie.
AcceptLanguageResolverResolves the active locale from the Accept-Language header.
SessionResolverResolves the active locale from the current session.

§Quick start

use modo::template::{Engine, TemplateConfig, TemplateContextLayer};

// Build the engine once at startup.
let engine = Engine::builder()
    .config(TemplateConfig::default())
    .build()
    .expect("failed to build engine");

// Serve static files and inject per-request context.
// `Engine` is cheaply cloneable (internal `Arc`).
let router: axum::Router = axum::Router::new()
    .merge(engine.static_service())
    .layer(TemplateContextLayer::new(engine.clone()));

Macros§

context
Creates a template context from keys and values or merging in another value.

Structs§

AcceptLanguageResolver
Resolves the active locale from the Accept-Language HTTP header.
CookieResolver
Resolves the active locale from a cookie.
Engine
The template engine.
EngineBuilder
Builder for Engine.
HxRequest
Axum extractor that detects HTMX requests.
QueryParamResolver
Resolves the active locale from a URL query parameter.
Renderer
Axum extractor for rendering MiniJinja templates.
SessionResolver
Resolves the active locale from the session data.
TemplateConfig
Configuration for the template engine.
TemplateContext
Per-request template context shared between middleware and handlers.
TemplateContextLayer
Tower middleware layer that populates TemplateContext for every request.

Traits§

LocaleResolver
Trait for extracting the active locale from a request.