1 use std
::collections
::BTreeMap
;
3 use helpers
::HelperDef
;
4 use registry
::Registry
;
5 use context
::{JsonTruthy, to_json}
;
6 use render
::{Renderable, RenderContext, RenderError, Helper}
;
11 impl HelperDef
for WithHelper
{
12 fn call(&self, h
: &Helper
, r
: &Registry
, rc
: &mut RenderContext
) -> Result
<(), RenderError
> {
14 try
!(h
.param(0).ok_or_else(|| RenderError
::new("Param not found for helper \"with\"")));
16 rc
.promote_local_vars();
19 let mut local_rc
= rc
.derive();
21 let not_empty
= param
.value().is_truthy();
22 let template
= if not_empty { h.template() }
else { h.inverse() }
;
24 if let Some(path_root
) = param
.path_root() {
25 let local_path_root
= format
!("{}/{}", local_rc
.get_path(), path_root
);
26 local_rc
.push_local_path_root(local_path_root
);
29 if let Some(inner_path
) = param
.path() {
30 let new_path
= format
!("{}/{}", local_rc
.get_path(), inner_path
);
31 local_rc
.set_path(new_path
);
34 if let Some(block_param
) = h
.block_param() {
35 let mut map
= BTreeMap
::new();
36 map
.insert(block_param
.to_string(), to_json(param
.value()));
37 local_rc
.push_block_context(&map
);
41 let result
= match template
{
42 Some(t
) => t
.render(r
, &mut local_rc
),
46 if h
.block_param().is_some() {
47 local_rc
.pop_block_context();
50 if param
.path_root().is_some() {
51 local_rc
.pop_local_path_root();
57 rc
.demote_local_vars();
62 pub static WITH_HELPER
: WithHelper
= WithHelper
;
66 use registry
::Registry
;
86 city
: "Beijing".to_string(),
87 country
: "China".to_string(),
91 name
: "Ning Sun".to_string(),
94 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
97 let mut handlebars
= Registry
::new();
98 assert
!(handlebars
.register_template_string("t0", "{{#with addr}}{{city}}{{/with}}")
100 assert
!(handlebars
.register_template_string("t1",
101 "{{#with notfound}}hello{{else}}world{{/with}}")
103 assert
!(handlebars
.register_template_string("t2",
104 "{{#with addr/country}}{{this}}{{/with}}")
107 let r0
= handlebars
.render("t0", &person
);
108 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
110 let r1
= handlebars
.render("t1", &person
);
111 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
113 let r2
= handlebars
.render("t2", &person
);
114 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
118 fn test_with_block_param() {
120 city
: "Beijing".to_string(),
121 country
: "China".to_string(),
124 let person
= Person
{
125 name
: "Ning Sun".to_string(),
128 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
131 let mut handlebars
= Registry
::new();
132 assert
!(handlebars
.register_template_string("t0",
133 "{{#with addr as |a|}}{{a.city}}{{/with}}")
135 assert
!(handlebars
.register_template_string("t1", "{{#with notfound as |c|}}hello{{else}}world{{/with}}").is_ok());
136 assert
!(handlebars
.register_template_string("t2",
137 "{{#with addr/country as |t|}}{{t}}{{/with}}")
140 let r0
= handlebars
.render("t0", &person
);
141 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
143 let r1
= handlebars
.render("t1", &person
);
144 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
146 let r2
= handlebars
.render("t2", &person
);
147 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
151 fn test_with_in_each() {
153 city
: "Beijing".to_string(),
154 country
: "China".to_string(),
157 let person
= Person
{
158 name
: "Ning Sun".to_string(),
161 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
164 let addr2
= Address
{
165 city
: "Beijing".to_string(),
166 country
: "China".to_string(),
169 let person2
= Person
{
170 name
: "Ning Sun".to_string(),
173 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
176 let people
= vec
![person
, person2
];
178 let mut handlebars
= Registry
::new();
179 assert
!(handlebars
.register_template_string("t0", "{{#each this}}{{#with addr}}{{city}}{{/with}}{{/each}}").is_ok());
180 assert
!(handlebars
.register_template_string("t1", "{{#each this}}{{#with addr}}{{../age}}{{/with}}{{/each}}").is_ok());
181 assert
!(handlebars
.register_template_string("t2", "{{#each this}}{{#with addr}}{{@../index}}{{/with}}{{/each}}").is_ok());
183 let r0
= handlebars
.render("t0", &people
);
184 assert_eq
!(r0
.ok().unwrap(), "BeijingBeijing".to_string());
186 let r1
= handlebars
.render("t1", &people
);
187 assert_eq
!(r1
.ok().unwrap(), "2727".to_string());
189 let r2
= handlebars
.render("t2", &people
);
190 assert_eq
!(r2
.ok().unwrap(), "01".to_string());
195 let mut handlebars
= Registry
::new();
196 assert
!(handlebars
.register_template_string("t0",
197 "{{#with a}}{{#with b}}{{../../d}}{{/with}}{{/with}}")
199 let data
= btreemap
! {
200 "a".to_string() => to_json(&btreemap
! {
201 "b".to_string() => vec
![btreemap
!{"c".to_string() => vec![1]}
]
203 "d".to_string() => to_json(&1)
206 let r0
= handlebars
.render("t0", &data
);
207 assert_eq
!(r0
.ok().unwrap(), "1".to_string());