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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Compiled HTML views: server-rendered templates that are Rust code by the
//! time the binary exists.
//!
//! # Why the templates are compiled
//!
//! Every runtime template engine -- minijinja, tera, handlebars -- ships a
//! parser and an expression evaluator, and runs both **inside the request
//! path**. That is the machinery server-side template injection drives: give
//! a template engine a value that reaches its parser and the evaluator will
//! do what the value says, which is the shortest route there is from a form
//! field to remote code execution.
//!
//! [`askama`] compiles a template into a Rust function at build time. At
//! runtime there is no parser, no evaluator, and no template text -- only the
//! `write!` calls the compiler emitted. SSTI is not mitigated here, it is
//! structurally absent, and that is the entire reason this module names
//! askama.
//!
//! The trade, stated plainly: **editing a template requires a rebuild.**
//!
//! # What this module owns
//!
//! * [`View<T>`] -- a template value on its way to becoming a response.
//! * [`view`] -- the constructor a handler calls.
//! * [`ViewError`] -- the one failure a compiled template still has, and its
//! conversion into [`crate::Error`], which drops the detail into the log
//! rather than into the body.
//! * An `IntoResponse` impl, so a handler can return a view directly and a
//! render failure becomes a `500` that names neither the template nor its
//! path. `src/view/response.rs` states at length why that matters.
//! * A re-export of the certified [`askama`] crate, so an application targets
//! the version Arcature pins instead of resolving its own.
//!
//! # What it does not own
//!
//! Template syntax, inheritance, filters and the escaper are askama's, and
//! this module deliberately puts nothing in front of them. It is a seam, not
//! a wrapper.
//!
//! # Escaping
//!
//! Askama picks an escaper from the template's extension: `html`, `htm`,
//! `svg`, `xml`, `j2`, `jinja` and `jinja2` get the HTML escaper, everything
//! else gets none. A `{{ value }}` in an `.html` template is therefore
//! escaped unless the template says `{{ value|safe }}`, and a value carrying
//! a `<script>` comes out as text. That is checked by a test in this module
//! rather than taken on trust.
//!
//! # Naming the crate from outside
//!
//! `#[derive(Template)]` writes code that says `askama::`. Inside a crate
//! that depends on askama directly, that resolves. An application depending
//! only on Arcature has to point the derive at the re-export:
//!
//! ```
//! use arcature::view::{Template, view};
//!
//! #[derive(Template)]
//! #[template(
//! source = "<h1>{{ title }}</h1>",
//! ext = "html",
//! askama = arcature::askama
//! )]
//! struct Welcome {
//! title: String,
//! }
//!
//! let html = view(Welcome { title: "Hello".into() }).render().unwrap();
//! assert_eq!(html, "<h1>Hello</h1>");
//! ```
//!
//! An application that would rather write plain `#[derive(Template)]` can add
//! `askama` to its own `Cargo.toml`; the price is a second version number to
//! keep in step with the framework's.
pub use ViewError;
// The certified askama, re-exported so downstream code targets the version
// Arcature pins -- the same reason `lettre`, `sea_orm` and `validator` are
// re-exported. `Template` comes along by name because a `use` names every
// namespace at once: the trait and the derive macro share it.
pub use askama;
pub use Template;
/// A template value on its way to becoming an HTTP response.
///
/// `View` is a newtype, not a wrapper: it renders through askama and adds
/// nothing to the template language.
///
/// ```
/// use arcature::view::{Template, View};
///
/// #[derive(Template)]
/// #[template(source = "{{ n }} bottles", ext = "txt", askama = arcature::askama)]
/// struct Song {
/// n: u32,
/// }
///
/// let view = View::new(Song { n: 99 });
/// assert_eq!(view.render().unwrap(), "99 bottles");
/// ```
///
/// It also carries the two things a response needs and a compiled template
/// does not know: the status and the content type. See
/// [`status`](View::status) and [`content_type`](View::content_type).
/// Begin a view from a template value.
///
/// ```
/// use arcature::view::{Template, view};
///
/// #[derive(Template)]
/// #[template(
/// source = "<p>Hello, {{ name }}.</p>",
/// ext = "html",
/// askama = arcature::askama
/// )]
/// struct Greeting {
/// name: String,
/// }
///
/// let html = view(Greeting { name: "Ada".into() }).render().unwrap();
/// assert_eq!(html, "<p>Hello, Ada.</p>");
/// ```
pub