1 use crate::block
::{BlockContext, BlockParams}
;
2 use crate::context
::Context
;
3 use crate::error
::RenderError
;
4 use crate::helpers
::{HelperDef, HelperResult}
;
5 use crate::json
::value
::JsonTruthy
;
6 use crate::output
::Output
;
7 use crate::registry
::Registry
;
8 use crate::render
::{Helper, RenderContext, Renderable}
;
10 #[derive(Clone, Copy)]
11 pub struct WithHelper
;
13 impl HelperDef
for WithHelper
{
14 fn call
<'reg
: 'rc
, 'rc
>(
16 h
: &Helper
<'reg
, 'rc
>,
19 rc
: &mut RenderContext
<'reg
, 'rc
>,
24 .ok_or_else(|| RenderError
::new("Param not found for helper \"with\""))?
;
26 let not_empty
= param
.value().is_truthy(false);
27 let template
= if not_empty { h.template() }
else { h.inverse() }
;
30 let mut block
= BlockContext
::new();
32 let new_path
= param
.context_path();
33 if let Some(new_path
) = new_path
{
34 *block
.base_path_mut() = new_path
.clone();
37 if let Some(block_param
) = h
.block_param() {
38 let mut params
= BlockParams
::new();
39 if new_path
.is_some() {
40 params
.add_path(block_param
, Vec
::with_capacity(0))?
;
42 params
.add_value(block_param
, param
.value().clone())?
;
45 block
.set_block_params(params
);
51 let result
= match template
{
52 Some(t
) => t
.render(r
, ctx
, rc
, out
),
63 pub static WITH_HELPER
: WithHelper
= WithHelper
;
67 use crate::json
::value
::to_json
;
68 use crate::registry
::Registry
;
87 city
: "Beijing".to_string(),
88 country
: "China".to_string(),
92 name
: "Ning Sun".to_string(),
95 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
98 let mut handlebars
= Registry
::new();
100 .register_template_string("t0", "{{#with addr}}{{city}}{{/with}}")
103 .register_template_string("t1", "{{#with notfound}}hello{{else}}world{{/with}}")
106 .register_template_string("t2", "{{#with addr/country}}{{this}}{{/with}}")
109 let r0
= handlebars
.render("t0", &person
);
110 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
112 let r1
= handlebars
.render("t1", &person
);
113 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
115 let r2
= handlebars
.render("t2", &person
);
116 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
120 fn test_with_block_param() {
122 city
: "Beijing".to_string(),
123 country
: "China".to_string(),
126 let person
= Person
{
127 name
: "Ning Sun".to_string(),
130 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
133 let mut handlebars
= Registry
::new();
135 .register_template_string("t0", "{{#with addr as |a|}}{{a.city}}{{/with}}")
138 .register_template_string("t1", "{{#with notfound as |c|}}hello{{else}}world{{/with}}")
141 .register_template_string("t2", "{{#with addr/country as |t|}}{{t}}{{/with}}")
144 let r0
= handlebars
.render("t0", &person
);
145 assert_eq
!(r0
.ok().unwrap(), "Beijing".to_string());
147 let r1
= handlebars
.render("t1", &person
);
148 assert_eq
!(r1
.ok().unwrap(), "world".to_string());
150 let r2
= handlebars
.render("t2", &person
);
151 assert_eq
!(r2
.ok().unwrap(), "China".to_string());
155 fn test_with_in_each() {
157 city
: "Beijing".to_string(),
158 country
: "China".to_string(),
161 let person
= Person
{
162 name
: "Ning Sun".to_string(),
165 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
168 let addr2
= Address
{
169 city
: "Beijing".to_string(),
170 country
: "China".to_string(),
173 let person2
= Person
{
174 name
: "Ning Sun".to_string(),
177 titles
: vec
!["programmer".to_string(), "cartographier".to_string()],
180 let people
= vec
![person
, person2
];
182 let mut handlebars
= Registry
::new();
184 .register_template_string(
186 "{{#each this}}{{#with addr}}{{city}}{{/with}}{{/each}}"
190 .register_template_string(
192 "{{#each this}}{{#with addr}}{{../age}}{{/with}}{{/each}}"
196 .register_template_string(
198 "{{#each this}}{{#with addr}}{{@../index}}{{/with}}{{/each}}"
202 let r0
= handlebars
.render("t0", &people
);
203 assert_eq
!(r0
.ok().unwrap(), "BeijingBeijing".to_string());
205 let r1
= handlebars
.render("t1", &people
);
206 assert_eq
!(r1
.ok().unwrap(), "2727".to_string());
208 let r2
= handlebars
.render("t2", &people
);
209 assert_eq
!(r2
.ok().unwrap(), "01".to_string());
214 let mut handlebars
= Registry
::new();
216 .register_template_string("t0", "{{#with a}}{{#with b}}{{../../d}}{{/with}}{{/with}}")
218 let data
= btreemap
! {
219 "a".to_string() => to_json(&btreemap
! {
220 "b".to_string() => vec
![btreemap
!{"c".to_string() => vec![1]}
]
222 "d".to_string() => to_json(1)
225 let r0
= handlebars
.render("t0", &data
);
226 assert_eq
!(r0
.ok().unwrap(), "1".to_string());