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
extern crate mustache;
extern crate rustc_serialize;
use std::io::Write;
use std::path::Path;
use rustc_serialize::Encodable;
pub struct View;
impl View {
pub fn render<T: Encodable> (pathstr: &str, data: T) -> Vec<u8> {
let path = Path::new(pathstr);
let template = mustache::compile_path(path).unwrap();
let mut output: Vec<u8> = vec![];
template.render(&mut output, &data).unwrap();
output
}
}
#[test]
fn it_works() {
use std::collections::HashMap;
let mut hash = HashMap::new();
hash.insert("name", "hello world!");
let output = View::render("test.txt", hash);
assert!(String::from_utf8(output).unwrap() == "abcdefg hello world! higklmn.");
}