#engine

    Template engines behind one registry.

    Import as justjavac/fus/engine.

    • TemplateEnv holds every layout and include as source, plus the engines that render them: new, with_engine, register, add, engine_names.
    • TemplateEngine::custom(name, exts, add_template, render_str, render_named) adds a backend without changing the build pipeline.
    • engine_kind(name) maps the templateEngine setting to the default engine (MiniJinja, or Liquid).
    • render_source, render_template and render_layout are the three call sites the build uses. safe_html marks already-rendered HTML as trusted so it is not escaped a second time.

    Jinja is the default and .liquid files use Liquid automatically. The six site filters in filters.mbt (slugify, date, markdown, url, truncate, json) are registered on the Jinja environment only.

    EngineKind

    pub enum EngineKind {
    MiniJinja
    Liquid
    } derive(Eq)

    Which engine is the default for pages and extensionless template names.

    TemplateEngine

    pub struct TemplateEngine {
    name : String
    exts : Array[String]
    add_template : (String, String) -> Unit
    render_str : (String,
    Json
    ) -> String
    render_named : (String, String,
    Json
    , String?) -> String
    }

    One template backend.

    add_template registers a layout or include under its name so that include and extends can resolve it, render_str renders a page body (inline source) and render_named renders a stored template with an optional wrapped body injected as trusted HTML.

    TemplateEngine::custom

    fn TemplateEngine::custom(name : String, exts : Array[String], add_template : (String, String) -> Unit, render_str : (String,
    Json
    ) -> String, render_named : (String, String,
    Json
    , String?) -> String) -> TemplateEngine

    Build a custom engine.

    add_template is called once per layout and include, render_str renders a page body and render_named renders a stored template with an optional body. Register the result with TemplateEnv::register; the extension decides which files it owns (exts).

    TemplateEnv

    pub struct TemplateEnv {
    engines : Array[TemplateEngine]
    default_engine : EngineKind
    sources : Map[String, String]
    kinds : Map[String, String]
    }

    A template environment: the registered engines plus every template source.

    kinds remembers which engine each name belongs to, taken from the file it was loaded from. Without it, layout: base had to guess: the name carries no extension, so a _layouts/base.liquid was handed to the default engine and came out as <!-- template error -->.

    TemplateEnv::add

    fn TemplateEnv::add(self : TemplateEnv, name : StringView, source : StringView) -> Unit

    Register a template under one name (base, post, hello).

    Invalid syntax is swallowed: a typo in one layout reports at render time instead of hiding every other page. The name carries no engine; use add_with when the file it came from has an extension.

    TemplateEnv::add_with

    fn TemplateEnv::add_with(self : TemplateEnv, name : StringView, source : StringView, ext : StringView) -> Unit

    Register a template and remember the engine its file extension selects.

    ext is the bare extension (jinja, liquid, j2); "" leaves the name on the default engine. Every engine still receives the source, because include resolves inside the engine that is rendering — but the name itself now resolves to the engine of the file it came from.

    TemplateEnv::engine_names

    fn TemplateEnv::engine_names(self : TemplateEnv) -> Array[String]

    Names of the registered engines, in registration order.

    TemplateEnv::new

    Create an environment that renders with Jinja unless a file says otherwise.

    TemplateEnv::register

    fn TemplateEnv::register(self : TemplateEnv, engine : TemplateEngine) -> Unit

    Register an engine. Later engines win for a shared extension.

    TemplateEnv::with_engine

    fn TemplateEnv::with_engine(kind : EngineKind) -> TemplateEnv

    Create an environment with an explicit default engine.

    Both built-in engines are always registered, so .liquid files keep working even when Jinja is the default.

    engine_kind

    fn engine_kind(name : StringView) -> EngineKind

    Parse a templateEngine configuration value.

    render_layout

    fn render_layout(name : StringView, ctx :
    Json
    , body_html : StringView, env : TemplateEnv) -> String

    Render a layout with body and content bound to trusted HTML.

    render_source

    fn render_source(source : StringView, ctx :
    Json
    , env : TemplateEnv) -> String

    Render an inline template source, used for page bodies.

    render_source_of

    fn render_source_of(source : StringView, path : StringView, ctx :
    Json
    , env : TemplateEnv) -> String

    Render an inline template source with the engine of path.

    Page bodies use this: a post.liquid body is Liquid even when Jinja is the default, and a post.jinja body stays Jinja even when templateEngine is liquid. Extensions no engine owns (.md, .html) fall back to the default.

    render_template

    fn render_template(name : StringView, ctx :
    Json
    , env : TemplateEnv) -> String

    Render a registered template by name.

    safe_html

    fn safe_html(s : StringView) ->
    Json

    Mark a string as trusted HTML for templates that need it explicitly.