Skip to main content

Crate hemplate_axum

Crate hemplate_axum 

Source
Expand description

axum integration for hemplate templates.

Wrap any Hemplate type in HtmlTemplate (or StatusTemplate) to return it from an axum handler:

ⓘ
async fn home() -> HtmlTemplate<HomeView> {
    HomeView { title: "Welcome".into() }.into()
}

No blanket impl<T: Hemplate> IntoResponse for T exists — Rust’s coherence rules forbid it (foreign trait + foreign type + overlap with axum’s existing blanket impls). The wrapper is the canonical workaround, matching axum’s own Json / Html / Form newtypes.

§hemplate-axum

Axum response wrappers for Hemplate views. Requires Rust 1.91 or newer.

[dependencies]
hemplate = "0.6.0"
hemplate-axum = "0.6.0"

Every Hemplate package has the same version and pins the others exactly; use hemplate and hemplate-axum of the same release.

use hemplate::{Hemplate, RenderError};
use hemplate_axum::{HemplateExt, HtmlTemplate};

struct HomePage;

impl Hemplate for HomePage {
    fn render_into(&self, output: &mut dyn core::fmt::Write) -> Result<(), RenderError> {
        output.write_str("<h1>Home</h1>")?;
        Ok(())
    }
}

assert_eq!(HomePage.render()?, "<h1>Home</h1>");
let response: HtmlTemplate<HomePage> = HomePage.into_html();

The crate provides:

  • HtmlTemplate<T> for 200 OK HTML responses;
  • StatusTemplate<T> for HTML responses with an explicit status code;
  • HemplateExt convenience methods: into_html() and with_status(...).

The wrappers depend only on hemplate-runtime; applications can use the normal hemplate facade without pulling parser tooling into the response adapter.

§License

MIT

Structs§

HtmlTemplate
Wrapper that converts a Hemplate type into a 200 OK axum Response with Content-Type: text/html; charset=utf-8.
StatusTemplate
Wrapper for pairing a Hemplate type with an HTTP status code.

Traits§

HemplateExt
Ergonomic extension methods for any Hemplate type — chainable wrapper construction without naming the wrapper type at the call site.