trillium_askama/lib.rs
1#![forbid(unsafe_code)]
2#![deny(
3 clippy::dbg_macro,
4 missing_copy_implementations,
5 rustdoc::missing_crate_level_docs,
6 missing_debug_implementations,
7 missing_docs,
8 nonstandard_style,
9 unused_qualifications
10)]
11
12//! provides support for using the askama compile-time template library
13//! with trillium. see
14//! [https://github.com/djc/askama](https://github.com/djc/askama) for
15//! more information about using askama.
16//!
17//! ```
18//! use trillium::Conn;
19//! use trillium_askama::{AskamaConnExt, Template};
20//! use trillium_testing::TestServer;
21//!
22//! #[derive(Template)]
23//! #[template(path = "examples/hello.html")]
24//! struct HelloTemplate<'a> {
25//! name: &'a str,
26//! }
27//!
28//! async fn handler(conn: Conn) -> Conn {
29//! conn.render(HelloTemplate { name: "trillium" })
30//! }
31//!
32//! # trillium_testing::block_on(async {
33//! let app = TestServer::new(handler).await;
34//! app.get("/")
35//! .await
36//! .assert_ok()
37//! .assert_body("Hello, trillium!\n");
38//! # });
39//! ```
40
41pub use askama::{self, Template};
42use trillium::Status;
43
44/// extends trillium conns with the ability to render askama templates
45pub trait AskamaConnExt {
46 /// renders an askama template, halting the conn and setting a 200
47 /// status code. also sets the mime type based on the template
48 /// extension
49 fn render(self, template: impl Template) -> Self;
50}
51
52impl AskamaConnExt for trillium::Conn {
53 fn render(self, template: impl Template) -> Self {
54 match template.render() {
55 Ok(text) => self.ok(text),
56 Err(error) => {
57 log::error!("Askama render error: {error}");
58 self.with_status(Status::InternalServerError)
59 .with_state(error)
60 }
61 }
62 }
63}
64
65#[cfg(test)]
66#[doc = include_str!("../README.md")]
67mod readme {}