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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#[macro_use]
extern crate askama;
use askama::Template;
#[derive(Template)]
#[template(path = "base.html")]
struct BaseTemplate<'a> {
title: &'a str,
}
#[derive(Template)]
#[template(path = "child.html")]
struct ChildTemplate<'a> {
_parent: BaseTemplate<'a>,
}
#[test]
fn test_use_base_directly() {
let t = BaseTemplate { title: "Foo" };
assert_eq!(t.render().unwrap(), "Foo\n\nFoo\nCopyright 2017");
}
#[test]
fn test_simple_extends() {
let t = ChildTemplate {
_parent: BaseTemplate { title: "Bar" },
};
assert_eq!(
t.render().unwrap(),
"Bar\n(Bar) Content goes here\nFoo\nCopyright 2017"
);
}
pub mod parent {
use askama::Template;
#[derive(Template)]
#[template(path = "base.html")]
pub struct BaseTemplate<'a> {
pub title: &'a str,
}
}
pub mod child {
use super::parent::*;
use askama::Template;
#[derive(Template)]
#[template(path = "child.html")]
pub struct ChildTemplate<'a> {
pub _parent: BaseTemplate<'a>,
}
}
#[test]
fn test_different_module() {
let t = child::ChildTemplate {
_parent: parent::BaseTemplate { title: "a" },
};
assert_eq!(
t.render().unwrap(),
"a\n(a) Content goes here\nFoo\nCopyright 2017"
);
}
#[derive(Template)]
#[template(path = "nested-base.html")]
struct NestedBaseTemplate {}
#[derive(Template)]
#[template(path = "nested-child.html")]
struct NestedChildTemplate {
_parent: NestedBaseTemplate,
}
#[test]
fn test_nested_blocks() {
let t = NestedChildTemplate {
_parent: NestedBaseTemplate {},
};
assert_eq!(t.render().unwrap(), "\ndurpy\n");
}
#[derive(Template)]
#[template(path = "deep-base.html")]
struct DeepBaseTemplate {
year: u16,
}
#[derive(Template)]
#[template(path = "deep-mid.html")]
struct DeepMidTemplate {
_parent: DeepBaseTemplate,
title: String,
}
#[derive(Template)]
#[template(path = "deep-kid.html")]
struct DeepKidTemplate {
_parent: DeepMidTemplate,
item: String,
}
#[test]
fn test_deep() {
let t = DeepKidTemplate {
_parent: DeepMidTemplate {
_parent: DeepBaseTemplate {
year: 2018,
},
title: "Test".into(),
},
item: "Foo".into(),
};
assert_eq!(t.render().unwrap(), "
<html>
<head>
<script></script>
</head>
<body>
<div id=\"wrap\">
<section id=\"content\">
Foo Foo Foo
</section>
<section id=\"nav\">
nav nav nav
</section>
</div>
</body>
</html>");
assert_eq!(t._parent.render().unwrap(), "
<html>
<head>
Test
</head>
<body>
<div id=\"wrap\">
<section id=\"content\">
No content found
</section>
<section id=\"nav\">
nav nav nav
</section>
</div>
</body>
</html>");
assert_eq!(t._parent._parent.render().unwrap(), "
<html>
<head>
<style></style>
</head>
<body>
nav nav nav
Copyright 2018
</body>
</html>");
}
|