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
#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::{fmt, io};
use askama::Template;
struct DisplayAdaptor<'a, T: Template>(&'a T);
impl<T: Template> fmt::Display for DisplayAdaptor<'_, T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.render_into(f).map_err(|_| fmt::Error)
}
}
#[inline]
pub fn render(tmpl: &impl Template, dest: impl io::Write) -> Result<(), io::Error> {
html5ever_arena_dom::render(&DisplayAdaptor(tmpl), dest)
}
#[cfg(test)]
mod tests {
use askama::Template;
#[test]
fn test_render_simple_invocation() {
#[derive(Template)]
#[template(ext = "html", source = "<EM>Test&{{index}}>!")]
struct TestTemplate {
index: u32,
}
let mut data = Vec::default();
super::render(&TestTemplate { index: 1 }, &mut data).expect("Rendered ok");
let data = std::str::from_utf8(&data).expect("Valid UTF-8");
assert_eq!(
data,
"<html><head></head><body><em>Test&1>!</em></body></html>"
);
}
}