1 use std
::collections
::BTreeMap
;
4 use error
::RenderError
;
5 use helpers
::{HelperDef, HelperResult}
;
7 use registry
::Registry
;
8 use render
::{Helper, RenderContext, Renderable}
;
9 use value
::{to_json, JsonTruthy}
;
11 #[derive(Clone, Copy)]
12 pub struct WithHelper
;
14 impl HelperDef
for WithHelper
{
15 fn call
<'reg
: 'rc
, 'rc
>(
20 rc
: &mut RenderContext
,
25 .ok_or_else(|| RenderError
::new("Param not found for helper \"with\""))?
;
27 rc
.promote_local_vars();
30 let mut local_rc
= rc
.derive();
32 let not_empty
= param
.value().is_truthy(false);
33 let template
= if not_empty
{
39 if let Some(path_root
) = param
.path_root() {
40 let local_path_root
= format
!("{}/{}", local_rc
.get_path(), path_root
);
41 local_rc
.push_local_path_root(local_path_root
);
44 if let Some(inner_path
) = param
.path() {
45 let new_path
= format
!("{}/{}", local_rc
.get_path(), inner_path
);
46 local_rc
.set_path(new_path
);
49 if let Some(block_param
) = h
.block_param() {
50 let mut map
= BTreeMap
::new();
51 map
.insert(block_param
.to_string(), to_json(param
.value()));
52 local_rc
.push_block_context(&map
)?
;
56 let result
= match template
{
57 Some(t
) => t
.render(r
, ctx
, &mut local_rc
, out
),
61 if h
.block_param().is_some() {
62 local_rc
.pop_block_context();
65 if param
.path_root().is_some() {
66 local_rc
.pop_local_path_root();
72 rc
.demote_local_vars();
77 pub static WITH_HELPER
: WithHelper
= WithHelper
;
81 use registry
::Registry
;
101 city
: "Beijing".to_string(),
102 country
: "China".to_string(),
105 let person
= Person
{
106 name
: "Ning Sun".to_string(),
109 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
112 let mut handlebars
= Registry
::new();
115 .register_template_string("t0", "{{#with addr}}{{city}}{{/with}}")
120 .register_template_string("t1", "{{#with notfound}}hello{{else}}world{{/with}}")
125 .register_template_string("t2", "{{#with addr/country}}{{this}}{{/with}}")
129 let r0
= handlebars
.render("t0", &person
);
130 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
132 let r1
= handlebars
.render("t1", &person
);
133 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
135 let r2
= handlebars
.render("t2", &person
);
136 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
140 fn test_with_block_param() {
142 city
: "Beijing".to_string(),
143 country
: "China".to_string(),
146 let person
= Person
{
147 name
: "Ning Sun".to_string(),
150 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
153 let mut handlebars
= Registry
::new();
156 .register_template_string("t0", "{{#with addr as |a|}}{{a.city}}{{/with}}")
161 .register_template_string(
163 "{{#with notfound as |c|}}hello{{else}}world{{/with}}"
169 .register_template_string("t2", "{{#with addr/country as |t|}}{{t}}{{/with}}")
173 let r0
= handlebars
.render("t0", &person
);
174 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
176 let r1
= handlebars
.render("t1", &person
);
177 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
179 let r2
= handlebars
.render("t2", &person
);
180 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
184 fn test_with_in_each() {
186 city
: "Beijing".to_string(),
187 country
: "China".to_string(),
190 let person
= Person
{
191 name
: "Ning Sun".to_string(),
194 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
197 let addr2
= Address
{
198 city
: "Beijing".to_string(),
199 country
: "China".to_string(),
202 let person2
= Person
{
203 name
: "Ning Sun".to_string(),
206 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
209 let people
= vec
![person
, person2
];
211 let mut handlebars
= Registry
::new();
214 .register_template_string(
216 "{{#each this}}{{#with addr}}{{city}}{{/with}}{{/each}}"
222 .register_template_string(
224 "{{#each this}}{{#with addr}}{{../age}}{{/with}}{{/each}}"
230 .register_template_string(
232 "{{#each this}}{{#with addr}}{{@../index}}{{/with}}{{/each}}"
237 let r0
= handlebars
.render("t0", &people
);
238 assert_eq
!(r0
.ok().unwrap(), "BeijingBeijing".to_string());
240 let r1
= handlebars
.render("t1", &people
);
241 assert_eq
!(r1
.ok().unwrap(), "2727".to_string());
243 let r2
= handlebars
.render("t2", &people
);
244 assert_eq
!(r2
.ok().unwrap(), "01".to_string());
249 let mut handlebars
= Registry
::new();
252 .register_template_string(
254 "{{#with a}}{{#with b}}{{../../d}}{{/with}}{{/with}}"
258 let data
= btreemap
! {
259 "a".to_string() => to_json(&btreemap
! {
260 "b".to_string() => vec
![btreemap
!{"c".to_string() => vec![1]}
]
262 "d".to_string() => to_json(1)
265 let r0
= handlebars
.render("t0", &data
);
266 assert_eq
!(r0
.ok().unwrap(), "1".to_string());