-
Notifications
You must be signed in to change notification settings - Fork 0
/
macros.rs
80 lines (69 loc) · 1.8 KB
/
macros.rs
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
#[macro_use]
extern crate tenjin;
use std::io;
use tenjin::*;
struct Context<'a> {
header: &'a str,
people: &'a [User<'a>],
}
struct User<'a> {
first: &'a str,
last: &'a str,
weight: usize,
html: &'a str,
}
context! {
self: ('a) Context<'a> {
header => @raw self.header, // Will NOT be escaped.
people => @iter self.people,
}
self: ('a) User<'a> {
name => @{
first => self.first,
last => self.last,
},
weight => self.weight,
html => self.html, // WILL be escaped.
}
}
fn main() {
let mut tenjin = Tenjin::empty();
tenjin.register("test", Template::compile("
{ header }
First Name: {{ person.name.first }}
Last Name: {{ person.name.last }}
Weight: {{ person.weight }} kg
{ for person in people }
First Name: { person.name.first }
Last Name: { person.name.last }
Weight: { person.weight } kg
Fav. HTML: { person.html }
{ end }
").unwrap());
let data = Context {
header: "<h1>Random Characters</h1>",
people: &[
User {
first: "Eren",
last: "Jaeger",
weight: 63,
html: "<strong>"
},
User {
first: "Mikasa",
last: "Ackerman",
weight: 68,
html: "<em></em>"
},
User {
first: "Armin",
last: "Arlert",
weight: 55,
html: "<pre> Tactical genius? </pre>"
},
],
};
let output = io::stdout();
let template = tenjin.get("test").unwrap();
tenjin.render(template, &data, &mut output.lock()).unwrap();
}