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
use std::collections::HashMap;
use std::io::prelude::*;
use std::fs::File;
use glob::glob;
use template::Template;
use context::Context;
use errors::TeraResult;
#[derive(Debug)]
pub struct Tera {
pub templates: HashMap<String, Template>,
}
impl Tera {
pub fn new(dir: &str) -> Tera {
if dir.find('*').is_none() {
panic!("Tera expects a glob as input, no * were found in {}", dir);
}
let mut templates = HashMap::new();
for entry in glob(dir).unwrap().filter_map(|e| e.ok()) {
let path = entry.as_path();
if path.is_file() {
let parent_dir = dir.split_at(dir.find('*').unwrap()).0;
let filepath = path.to_string_lossy().replace(parent_dir, "");
let mut f = File::open(path).unwrap();
let mut input = String::new();
f.read_to_string(&mut input).unwrap();
templates.insert(filepath.to_owned(), Template::new(&filepath, &input));
}
}
Tera {
templates: templates
}
}
pub fn render(&self, template_name: &str, data: Context) -> TeraResult<String> {
let template = self.templates.get(template_name).unwrap();
template.render(data, self.templates.clone())
}
pub fn get_template(&self, template_name: &str) -> Option<&Template> {
self.templates.get(template_name)
}
}