1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#![forbid(unsafe_code)]
#![deny(
    clippy::dbg_macro,
    missing_copy_implementations,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    unused_qualifications
)]

/*!
provides support for using the askama compile-time template library
with trillium.  see
[https://github.com/djc/askama](https://github.com/djc/askama) for
more information about using askama.

```
use trillium::Conn;
use trillium_askama::{AskamaConnExt, Template};

#[derive(Template)]
#[template(path = "examples/hello.html")]
struct HelloTemplate<'a> {
    name: &'a str,
}

async fn handler(conn: Conn) -> Conn {
    conn.render(HelloTemplate { name: "trillium" })
}

use trillium_testing::prelude::*;
assert_ok!(
    get("/").on(&handler),
    "Hello, trillium!",
    "content-type" => "text/html"
);
```
*/

pub use askama;
pub use askama::Template;
use trillium::KnownHeaderName::ContentType;

/// extends trillium conns with the ability to render askama templates
pub trait AskamaConnExt {
    /// renders an askama template, halting the conn and setting a 200
    /// status code. also sets the mime type based on the template
    /// extension
    fn render(self, template: impl Template) -> Self;
}

impl AskamaConnExt for trillium::Conn {
    fn render(mut self, template: impl Template) -> Self {
        use askama::DynTemplate;
        let text = template.render().unwrap();
        if let Some(extension) = template.extension() {
            if let Some(mime) = mime_db::lookup(extension) {
                self.headers_mut().insert(ContentType, mime);
            }
        }

        self.ok(text)
    }
}