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
use std::fs::read_dir;
use handlebars::Handlebars;
use std::path::Path;
use failure::Error;
use Config;
pub fn load_all(config: &Config) -> Result<Handlebars, Error> {
let mut handlebars = Handlebars::new();
register_all_templates(config, &mut handlebars)?;
Ok(handlebars)
}
fn register_all_templates(config: &Config, handlebars: &mut Handlebars) -> Result<(), Error> {
let templates_path = config.templates_path;
let paths = read_dir(templates_path)?;
for path in paths {
let uw = path?;
let relative = uw.path().display().to_string();
if relative.ends_with(".hbs") {
println!("registering template: {}", &relative);
let filename_option = path_to_filename(&relative);
match filename_option
{
Some(filename) => handlebars.register_template_file(&filename, &Path::new(&relative))?,
None => bail!("fail to get filename")
}
}
}
Ok(())
}
fn path_to_filename(relative_path: &str) -> Option<String> {
let path = relative_path
.split('/')
.last()?
.split('.')
.next()?
.to_string();
Some(path)
}