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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
#[macro_use]
extern crate askama;
use askama::Template;
#[derive(Template)]
#[template(path = "simple.html")]
struct VariablesTemplate<'a> {
strvar: &'a str,
num: i64,
i18n: String,
}
#[test]
fn test_variables() {
let s = VariablesTemplate {
strvar: "foo",
num: 42,
i18n: "Iñtërnâtiônàlizætiøn".to_string(),
};
assert_eq!(s.render(), "\nhello world, foo\n\
with number: 42\n\
Iñtërnâtiônàlizætiøn is important\n\
in vars too: Iñtërnâtiônàlizætiøn");
}
#[derive(Template)]
#[template(path = "if.html")]
struct IfTemplate {
cond: bool,
}
#[test]
fn test_if() {
let s = IfTemplate { cond: true };
assert_eq!(s.render(), "true");
}
#[derive(Template)]
#[template(path = "else.html")]
struct ElseTemplate {
cond: bool,
}
#[test]
fn test_else() {
let s = ElseTemplate { cond: false };
assert_eq!(s.render(), "false");
}
#[derive(Template)]
#[template(path = "else-if.html")]
struct ElseIfTemplate {
cond: bool,
check: bool,
}
#[test]
fn test_else_if() {
let s = ElseIfTemplate { cond: false, check: true };
assert_eq!(s.render(), "checked");
}
#[derive(Template)]
#[template(path = "literals.html")]
struct LiteralsTemplate {}
#[test]
fn test_literals() {
let s = LiteralsTemplate {};
assert_eq!(s.render(), "a");
}
struct Holder {
a: usize,
}
#[derive(Template)]
#[template(path = "attr.html")]
struct AttrTemplate {
inner: Holder,
}
#[test]
fn test_attr() {
let t = AttrTemplate { inner: Holder { a: 5 } };
assert_eq!(t.render(), "5");
}
struct NestedHolder {
holder: Holder,
}
#[derive(Template)]
#[template(path = "nested-attr.html")]
struct NestedAttrTemplate {
inner: NestedHolder,
}
#[test]
fn test_nested_attr() {
let t = NestedAttrTemplate {
inner: NestedHolder { holder: Holder { a: 5 } }
};
assert_eq!(t.render(), "5");
}
#[derive(Template)]
#[template(path = "option.html")]
struct OptionTemplate<'a> {
var: Option<&'a str>,
}
#[test]
fn test_option() {
let some = OptionTemplate { var: Some("foo") };
assert_eq!(some.render(), "some: foo");
let none = OptionTemplate { var: None };
assert_eq!(none.render(), "none");
}
#[derive(Template)]
#[template(path = "generics.html")]
struct GenericsTemplate<T: std::fmt::Display, U = u8>
where U: std::fmt::Display {
t: T,
u: U,
}
#[test]
fn test_generics() {
let t = GenericsTemplate { t: "a", u: 42 };
assert_eq!(t.render(), "a42");
}
#[derive(Template)]
#[template(path = "json.html")]
struct JsonTemplate<'a> {
foo: &'a str,
bar: &'a str,
}
#[test]
fn test_json() {
let t = JsonTemplate { foo: "a", bar: "b" };
assert_eq!(t.render(), "{\"foo\": \"a\", \"bar\": \"b\"}");
}
|