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
mod square_brackets;
#[cfg(test)]
mod tests;
mod call_stack;
mod for_loop;
mod macros;
mod processor;
mod stack_frame;
use std::io::Write;
use self::processor::Processor;
use crate::errors::Result;
use crate::template::Template;
use crate::tera::Tera;
use crate::utils::buffer_to_string;
use crate::Context;
#[derive(Debug)]
pub struct Renderer<'a> {
template: &'a Template,
tera: &'a Tera,
context: &'a Context,
should_escape: bool,
}
impl<'a> Renderer<'a> {
#[inline]
pub fn new(template: &'a Template, tera: &'a Tera, context: &'a Context) -> Renderer<'a> {
let should_escape = tera.autoescape_suffixes.iter().any(|ext| {
if let Some(ref p) = template.path {
return p.ends_with(ext);
}
template.name.ends_with(ext)
});
Renderer { template, tera, context, should_escape }
}
pub fn render(&self) -> Result<String> {
let mut output = Vec::with_capacity(2000);
self.render_to(&mut output)?;
buffer_to_string(|| "converting rendered buffer to string".to_string(), output)
}
pub fn render_to(&self, mut output: impl Write) -> Result<()> {
let mut processor =
Processor::new(self.template, self.tera, self.context, self.should_escape);
processor.render(&mut output)
}
}