| 1 | use serde_json::value::Value as Json; |
| 2 | |
| 3 | use super::block_util::create_block; |
| 4 | use crate::block::{BlockContext, BlockParams}; |
| 5 | use crate::context::Context; |
| 6 | use crate::error::RenderError; |
| 7 | use crate::helpers::{HelperDef, HelperResult}; |
| 8 | use crate::json::value::to_json; |
| 9 | use crate::output::Output; |
| 10 | use crate::registry::Registry; |
| 11 | use crate::render::{Helper, RenderContext, Renderable}; |
| 12 | use crate::util::copy_on_push_vec; |
| 13 | use crate::RenderErrorReason; |
| 14 | |
| 15 | fn update_block_context( |
| 16 | block: &mut BlockContext<'_>, |
| 17 | base_path: Option<&Vec<String>>, |
| 18 | relative_path: String, |
| 19 | is_first: bool, |
| 20 | value: &Json, |
| 21 | ) { |
| 22 | if let Some(p) = base_path { |
| 23 | if is_first { |
| 24 | *block.base_path_mut() = copy_on_push_vec(p, relative_path); |
| 25 | } else if let Some(ptr) = block.base_path_mut().last_mut() { |
| 26 | *ptr = relative_path; |
| 27 | } |
| 28 | } else { |
| 29 | block.set_base_value(value.clone()); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | fn set_block_param<'rc>( |
| 34 | block: &mut BlockContext<'rc>, |
| 35 | h: &Helper<'rc>, |
| 36 | base_path: Option<&Vec<String>>, |
| 37 | k: &Json, |
| 38 | v: &Json, |
| 39 | ) -> Result<(), RenderError> { |
| 40 | if let Some(bp_val) = h.block_param() { |
| 41 | let mut params = BlockParams::new(); |
| 42 | if base_path.is_some() { |
| 43 | params.add_path(bp_val, Vec::with_capacity(0))?; |
| 44 | } else { |
| 45 | params.add_value(bp_val, v.clone())?; |
| 46 | } |
| 47 | |
| 48 | block.set_block_params(params); |
| 49 | } else if let Some((bp_val, bp_key)) = h.block_param_pair() { |
| 50 | let mut params = BlockParams::new(); |
| 51 | if base_path.is_some() { |
| 52 | params.add_path(bp_val, Vec::with_capacity(0))?; |
| 53 | } else { |
| 54 | params.add_value(bp_val, v.clone())?; |
| 55 | } |
| 56 | params.add_value(bp_key, k.clone())?; |
| 57 | |
| 58 | block.set_block_params(params); |
| 59 | } |
| 60 | |
| 61 | Ok(()) |
| 62 | } |
| 63 | |
| 64 | #[derive(Clone, Copy)] |
| 65 | pub struct EachHelper; |
| 66 | |
| 67 | impl HelperDef for EachHelper { |
| 68 | fn call<'reg: 'rc, 'rc>( |
| 69 | &self, |
| 70 | h: &Helper<'rc>, |
| 71 | r: &'reg Registry<'reg>, |
| 72 | ctx: &'rc Context, |
| 73 | rc: &mut RenderContext<'reg, 'rc>, |
| 74 | out: &mut dyn Output, |
| 75 | ) -> HelperResult { |
| 76 | let value = h |
| 77 | .param(0) |
| 78 | .ok_or(RenderErrorReason::ParamNotFoundForIndex("each", 0))?; |
| 79 | |
| 80 | let template = h.template(); |
| 81 | |
| 82 | match template { |
| 83 | Some(t) => match *value.value() { |
| 84 | Json::Array(ref list) |
| 85 | if !list.is_empty() || (list.is_empty() && h.inverse().is_none()) => |
| 86 | { |
| 87 | let block_context = create_block(value); |
| 88 | rc.push_block(block_context); |
| 89 | |
| 90 | let len = list.len(); |
| 91 | |
| 92 | let array_path = value.context_path(); |
| 93 | |
| 94 | for (i, v) in list.iter().enumerate().take(len) { |
| 95 | if let Some(ref mut block) = rc.block_mut() { |
| 96 | let is_first = i == 0usize; |
| 97 | let is_last = i == len - 1; |
| 98 | |
| 99 | let index = to_json(i); |
| 100 | block.set_local_var("first", to_json(is_first)); |
| 101 | block.set_local_var("last", to_json(is_last)); |
| 102 | block.set_local_var("index", index.clone()); |
| 103 | |
| 104 | update_block_context(block, array_path, i.to_string(), is_first, v); |
| 105 | set_block_param(block, h, array_path, &index, v)?; |
| 106 | } |
| 107 | |
| 108 | t.render(r, ctx, rc, out)?; |
| 109 | } |
| 110 | |
| 111 | rc.pop_block(); |
| 112 | Ok(()) |
| 113 | } |
| 114 | Json::Object(ref obj) |
| 115 | if !obj.is_empty() || (obj.is_empty() && h.inverse().is_none()) => |
| 116 | { |
| 117 | let block_context = create_block(value); |
| 118 | rc.push_block(block_context); |
| 119 | |
| 120 | let len = obj.len(); |
| 121 | |
| 122 | let obj_path = value.context_path(); |
| 123 | |
| 124 | for (i, (k, v)) in obj.iter().enumerate() { |
| 125 | if let Some(ref mut block) = rc.block_mut() { |
| 126 | let is_first = i == 0usize; |
| 127 | let is_last = i == len - 1; |
| 128 | |
| 129 | let key = to_json(k); |
| 130 | block.set_local_var("first", to_json(is_first)); |
| 131 | block.set_local_var("last", to_json(is_last)); |
| 132 | block.set_local_var("key", key.clone()); |
| 133 | |
| 134 | update_block_context(block, obj_path, k.to_string(), is_first, v); |
| 135 | set_block_param(block, h, obj_path, &key, v)?; |
| 136 | } |
| 137 | |
| 138 | t.render(r, ctx, rc, out)?; |
| 139 | } |
| 140 | |
| 141 | rc.pop_block(); |
| 142 | Ok(()) |
| 143 | } |
| 144 | _ => { |
| 145 | if let Some(else_template) = h.inverse() { |
| 146 | else_template.render(r, ctx, rc, out) |
| 147 | } else if r.strict_mode() { |
| 148 | Err(RenderError::strict_error(value.relative_path())) |
| 149 | } else { |
| 150 | Ok(()) |
| 151 | } |
| 152 | } |
| 153 | }, |
| 154 | None => Ok(()), |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | pub static EACH_HELPER: EachHelper = EachHelper; |
| 160 | |
| 161 | #[cfg(test)] |
| 162 | mod test { |
| 163 | use crate::registry::Registry; |
| 164 | use serde_json::value::Value as Json; |
| 165 | use std::collections::BTreeMap; |
| 166 | use std::str::FromStr; |
| 167 | |
| 168 | #[test] |
| 169 | fn test_empty_each() { |
| 170 | let mut hbs = Registry::new(); |
| 171 | hbs.set_strict_mode(true); |
| 172 | |
| 173 | let data = json!({ |
| 174 | "a": [ ], |
| 175 | }); |
| 176 | |
| 177 | let template = "{{#each a}}each{{/each}}"; |
| 178 | assert_eq!(hbs.render_template(template, &data).unwrap(), ""); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn test_each() { |
| 183 | let mut handlebars = Registry::new(); |
| 184 | assert!(handlebars |
| 185 | .register_template_string( |
| 186 | "t0", |
| 187 | "{{#each this}}{{@first}}|{{@last}}|{{@index}}:{{this}}|{{/each}}", |
| 188 | ) |
| 189 | .is_ok()); |
| 190 | assert!(handlebars |
| 191 | .register_template_string( |
| 192 | "t1", |
| 193 | "{{#each this}}{{@first}}|{{@last}}|{{@key}}:{{this}}|{{/each}}", |
| 194 | ) |
| 195 | .is_ok()); |
| 196 | |
| 197 | let r0 = handlebars.render("t0", &vec![1u16, 2u16, 3u16]); |
| 198 | assert_eq!( |
| 199 | r0.ok().unwrap(), |
| 200 | "true|false|0:1|false|false|1:2|false|true|2:3|".to_string() |
| 201 | ); |
| 202 | |
| 203 | let mut m: BTreeMap<String, u16> = BTreeMap::new(); |
| 204 | m.insert("ftp".to_string(), 21); |
| 205 | m.insert("gopher".to_string(), 70); |
| 206 | m.insert("http".to_string(), 80); |
| 207 | let r1 = handlebars.render("t1", &m); |
| 208 | assert_eq!( |
| 209 | r1.ok().unwrap(), |
| 210 | "true|false|ftp:21|false|false|gopher:70|false|true|http:80|".to_string() |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn test_each_with_parent() { |
| 216 | let json_str = r#"{"a":{"a":99,"c":[{"d":100},{"d":200}]}}"#; |
| 217 | |
| 218 | let data = Json::from_str(json_str).unwrap(); |
| 219 | // println!("data: {}", data); |
| 220 | let mut handlebars = Registry::new(); |
| 221 | |
| 222 | // previously, to access the parent in an each block, |
| 223 | // a user would need to specify ../../b, as the path |
| 224 | // that is computed includes the array index: ./a.c.[0] |
| 225 | assert!(handlebars |
| 226 | .register_template_string("t0", "{{#each a.c}} d={{d}} b={{../a.a}} {{/each}}") |
| 227 | .is_ok()); |
| 228 | |
| 229 | let r1 = handlebars.render("t0", &data); |
| 230 | assert_eq!(r1.ok().unwrap(), " d=100 b=99 d=200 b=99 ".to_string()); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn test_nested_each_with_parent() { |
| 235 | let json_str = r#"{"a": [{"b": [{"d": 100}], "c": 200}]}"#; |
| 236 | |
| 237 | let data = Json::from_str(json_str).unwrap(); |
| 238 | let mut handlebars = Registry::new(); |
| 239 | assert!(handlebars |
| 240 | .register_template_string( |
| 241 | "t0", |
| 242 | "{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}", |
| 243 | ) |
| 244 | .is_ok()); |
| 245 | |
| 246 | let r1 = handlebars.render("t0", &data); |
| 247 | assert_eq!(r1.ok().unwrap(), "100:200".to_string()); |
| 248 | } |
| 249 | |
| 250 | #[test] |
| 251 | fn test_nested_each() { |
| 252 | let json_str = r#"{"a": [{"b": true}], "b": [[1, 2, 3],[4, 5]]}"#; |
| 253 | |
| 254 | let data = Json::from_str(json_str).unwrap(); |
| 255 | |
| 256 | let mut handlebars = Registry::new(); |
| 257 | assert!(handlebars |
| 258 | .register_template_string( |
| 259 | "t0", |
| 260 | "{{#each b}}{{#if ../a}}{{#each this}}{{this}}{{/each}}{{/if}}{{/each}}", |
| 261 | ) |
| 262 | .is_ok()); |
| 263 | |
| 264 | let r1 = handlebars.render("t0", &data); |
| 265 | assert_eq!(r1.ok().unwrap(), "12345".to_string()); |
| 266 | } |
| 267 | |
| 268 | #[test] |
| 269 | fn test_nested_array() { |
| 270 | let mut handlebars = Registry::new(); |
| 271 | assert!(handlebars |
| 272 | .register_template_string("t0", "{{#each this.[0]}}{{this}}{{/each}}") |
| 273 | .is_ok()); |
| 274 | |
| 275 | let r0 = handlebars.render("t0", &(vec![vec![1, 2, 3]])); |
| 276 | |
| 277 | assert_eq!(r0.ok().unwrap(), "123".to_string()); |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn test_empty_key() { |
| 282 | let mut handlebars = Registry::new(); |
| 283 | assert!(handlebars |
| 284 | .register_template_string("t0", "{{#each this}}{{@key}}-{{value}}\n{{/each}}") |
| 285 | .is_ok()); |
| 286 | |
| 287 | let r0 = handlebars |
| 288 | .render( |
| 289 | "t0", |
| 290 | &json!({ |
| 291 | "foo": { |
| 292 | "value": "bar" |
| 293 | }, |
| 294 | "": { |
| 295 | "value": "baz" |
| 296 | } |
| 297 | }), |
| 298 | ) |
| 299 | .unwrap(); |
| 300 | |
| 301 | let mut r0_sp: Vec<_> = r0.split('\n').collect(); |
| 302 | r0_sp.sort(); |
| 303 | |
| 304 | assert_eq!(r0_sp, vec!["", "-baz", "foo-bar"]); |
| 305 | } |
| 306 | |
| 307 | #[test] |
| 308 | fn test_each_else() { |
| 309 | let mut handlebars = Registry::new(); |
| 310 | assert!(handlebars |
| 311 | .register_template_string("t0", "{{#each a}}1{{else}}empty{{/each}}") |
| 312 | .is_ok()); |
| 313 | let m1 = json!({ |
| 314 | "a": [] |
| 315 | }); |
| 316 | let r0 = handlebars.render("t0", &m1).unwrap(); |
| 317 | assert_eq!(r0, "empty"); |
| 318 | |
| 319 | let m2 = json!({ |
| 320 | "b": [] |
| 321 | }); |
| 322 | let r1 = handlebars.render("t0", &m2).unwrap(); |
| 323 | assert_eq!(r1, "empty"); |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn test_block_param() { |
| 328 | let mut handlebars = Registry::new(); |
| 329 | assert!(handlebars |
| 330 | .register_template_string("t0", "{{#each a as |i|}}{{i}}{{/each}}") |
| 331 | .is_ok()); |
| 332 | let m1 = json!({ |
| 333 | "a": [1,2,3,4,5] |
| 334 | }); |
| 335 | let r0 = handlebars.render("t0", &m1).unwrap(); |
| 336 | assert_eq!(r0, "12345"); |
| 337 | } |
| 338 | |
| 339 | #[test] |
| 340 | fn test_each_object_block_param() { |
| 341 | let mut handlebars = Registry::new(); |
| 342 | let template = "{{#each this as |v k|}}\ |
| 343 | {{#with k as |inner_k|}}{{inner_k}}{{/with}}:{{v}}|\ |
| 344 | {{/each}}"; |
| 345 | assert!(handlebars.register_template_string("t0", template).is_ok()); |
| 346 | |
| 347 | let m = json!({ |
| 348 | "ftp": 21, |
| 349 | "http": 80 |
| 350 | }); |
| 351 | let r0 = handlebars.render("t0", &m); |
| 352 | assert_eq!(r0.ok().unwrap(), "ftp:21|http:80|".to_string()); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn test_each_object_block_param2() { |
| 357 | let mut handlebars = Registry::new(); |
| 358 | let template = "{{#each this as |v k|}}\ |
| 359 | {{#with v as |inner_v|}}{{k}}:{{inner_v}}{{/with}}|\ |
| 360 | {{/each}}"; |
| 361 | |
| 362 | assert!(handlebars.register_template_string("t0", template).is_ok()); |
| 363 | |
| 364 | let m = json!({ |
| 365 | "ftp": 21, |
| 366 | "http": 80 |
| 367 | }); |
| 368 | let r0 = handlebars.render("t0", &m); |
| 369 | assert_eq!(r0.ok().unwrap(), "ftp:21|http:80|".to_string()); |
| 370 | } |
| 371 | |
| 372 | fn test_nested_each_with_path_ups() { |
| 373 | let mut handlebars = Registry::new(); |
| 374 | assert!(handlebars |
| 375 | .register_template_string( |
| 376 | "t0", |
| 377 | "{{#each a.b}}{{#each c}}{{../../d}}{{/each}}{{/each}}", |
| 378 | ) |
| 379 | .is_ok()); |
| 380 | |
| 381 | let data = json!({ |
| 382 | "a": { |
| 383 | "b": [{"c": [1]}] |
| 384 | }, |
| 385 | "d": 1 |
| 386 | }); |
| 387 | |
| 388 | let r0 = handlebars.render("t0", &data); |
| 389 | assert_eq!(r0.ok().unwrap(), "1".to_string()); |
| 390 | } |
| 391 | |
| 392 | #[test] |
| 393 | fn test_nested_each_with_path_up_this() { |
| 394 | let mut handlebars = Registry::new(); |
| 395 | let template = "{{#each variant}}{{#each ../typearg}}\ |
| 396 | {{#if @first}}template<{{/if}}{{this}}{{#if @last}}>{{else}},{{/if}}\ |
| 397 | {{/each}}{{/each}}"; |
| 398 | assert!(handlebars.register_template_string("t0", template).is_ok()); |
| 399 | let data = json!({ |
| 400 | "typearg": ["T"], |
| 401 | "variant": ["1", "2"] |
| 402 | }); |
| 403 | let r0 = handlebars.render("t0", &data); |
| 404 | assert_eq!(r0.ok().unwrap(), "template<T>template<T>".to_string()); |
| 405 | } |
| 406 | |
| 407 | #[test] |
| 408 | fn test_key_iteration_with_unicode() { |
| 409 | let mut handlebars = Registry::new(); |
| 410 | assert!(handlebars |
| 411 | .register_template_string("t0", "{{#each this}}{{@key}}: {{this}}\n{{/each}}") |
| 412 | .is_ok()); |
| 413 | let data = json!({ |
| 414 | "normal": 1, |
| 415 | "你好": 2, |
| 416 | "#special key": 3, |
| 417 | "😂": 4, |
| 418 | "me.dot.key": 5 |
| 419 | }); |
| 420 | let r0 = handlebars.render("t0", &data).ok().unwrap(); |
| 421 | assert!(r0.contains("normal: 1")); |
| 422 | assert!(r0.contains("你好: 2")); |
| 423 | assert!(r0.contains("#special key: 3")); |
| 424 | assert!(r0.contains("😂: 4")); |
| 425 | assert!(r0.contains("me.dot.key: 5")); |
| 426 | } |
| 427 | |
| 428 | #[test] |
| 429 | fn test_base_path_after_each() { |
| 430 | let mut handlebars = Registry::new(); |
| 431 | assert!(handlebars |
| 432 | .register_template_string("t0", "{{#each a}}{{this}}{{/each}} {{b}}") |
| 433 | .is_ok()); |
| 434 | let data = json!({ |
| 435 | "a": [1, 2, 3, 4], |
| 436 | "b": "good", |
| 437 | }); |
| 438 | |
| 439 | let r0 = handlebars.render("t0", &data).ok().unwrap(); |
| 440 | |
| 441 | assert_eq!("1234 good", r0); |
| 442 | } |
| 443 | |
| 444 | #[test] |
| 445 | fn test_else_context() { |
| 446 | let reg = Registry::new(); |
| 447 | let template = "{{#each list}}A{{else}}{{foo}}{{/each}}"; |
| 448 | let input = json!({"list": [], "foo": "bar"}); |
| 449 | let rendered = reg.render_template(template, &input).unwrap(); |
| 450 | assert_eq!("bar", rendered); |
| 451 | } |
| 452 | |
| 453 | #[test] |
| 454 | fn test_block_context_leak() { |
| 455 | let reg = Registry::new(); |
| 456 | let template = "{{#each list}}{{#each inner}}{{this}}{{/each}}{{foo}}{{/each}}"; |
| 457 | let input = json!({"list": [{"inner": [], "foo": 1}, {"inner": [], "foo": 2}]}); |
| 458 | let rendered = reg.render_template(template, &input).unwrap(); |
| 459 | assert_eq!("12", rendered); |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn test_derived_array_as_block_params() { |
| 464 | handlebars_helper!(range: |x: u64| (0..x).collect::<Vec<u64>>()); |
| 465 | let mut reg = Registry::new(); |
| 466 | reg.register_helper("range", Box::new(range)); |
| 467 | let template = "{{#each (range 3) as |i|}}{{i}}{{/each}}"; |
| 468 | let input = json!(0); |
| 469 | let rendered = reg.render_template(template, &input).unwrap(); |
| 470 | assert_eq!("012", rendered); |
| 471 | } |
| 472 | |
| 473 | #[test] |
| 474 | fn test_derived_object_as_block_params() { |
| 475 | handlebars_helper!(point: |x: u64, y: u64| json!({"x":x, "y":y})); |
| 476 | let mut reg = Registry::new(); |
| 477 | reg.register_helper("point", Box::new(point)); |
| 478 | let template = "{{#each (point 0 1) as |i|}}{{i}}{{/each}}"; |
| 479 | let input = json!(0); |
| 480 | let rendered = reg.render_template(template, &input).unwrap(); |
| 481 | assert_eq!("01", rendered); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn test_derived_array_without_block_param() { |
| 486 | handlebars_helper!(range: |x: u64| (0..x).collect::<Vec<u64>>()); |
| 487 | let mut reg = Registry::new(); |
| 488 | reg.register_helper("range", Box::new(range)); |
| 489 | let template = "{{#each (range 3)}}{{this}}{{/each}}"; |
| 490 | let input = json!(0); |
| 491 | let rendered = reg.render_template(template, &input).unwrap(); |
| 492 | assert_eq!("012", rendered); |
| 493 | } |
| 494 | |
| 495 | #[test] |
| 496 | fn test_derived_object_without_block_params() { |
| 497 | handlebars_helper!(point: |x: u64, y: u64| json!({"x":x, "y":y})); |
| 498 | let mut reg = Registry::new(); |
| 499 | reg.register_helper("point", Box::new(point)); |
| 500 | let template = "{{#each (point 0 1)}}{{this}}{{/each}}"; |
| 501 | let input = json!(0); |
| 502 | let rendered = reg.render_template(template, &input).unwrap(); |
| 503 | assert_eq!("01", rendered); |
| 504 | } |
| 505 | |
| 506 | #[test] |
| 507 | fn test_non_iterable() { |
| 508 | let reg = Registry::new(); |
| 509 | let template = "{{#each this}}each block{{else}}else block{{/each}}"; |
| 510 | let input = json!("strings aren't iterable"); |
| 511 | let rendered = reg.render_template(template, &input).unwrap(); |
| 512 | assert_eq!("else block", rendered); |
| 513 | } |
| 514 | |
| 515 | #[test] |
| 516 | fn test_render_array_without_trailig_commas() { |
| 517 | let reg = Registry::new(); |
| 518 | let template = "Array: {{array}}"; |
| 519 | let input = json!({"array": [1, 2, 3]}); |
| 520 | let rendered = reg.render_template(template, &input); |
| 521 | |
| 522 | assert_eq!("Array: [1, 2, 3]", rendered.unwrap()); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn test_recursion() { |
| 527 | let mut reg = Registry::new(); |
| 528 | assert!(reg |
| 529 | .register_template_string( |
| 530 | "walk", |
| 531 | "(\ |
| 532 | {{#each this}}\ |
| 533 | {{#if @key}}{{@key}}{{else}}{{@index}}{{/if}}: \ |
| 534 | {{this}} \ |
| 535 | {{> walk this}}, \ |
| 536 | {{/each}}\ |
| 537 | )", |
| 538 | ) |
| 539 | .is_ok()); |
| 540 | |
| 541 | let input = json!({ |
| 542 | "array": [42, {"wow": "cool"}, [[]]], |
| 543 | "object": { "a": { "b": "c", "d": ["e"] } }, |
| 544 | "string": "hi" |
| 545 | }); |
| 546 | let expected_output = "(\ |
| 547 | array: [42, [object], [[]]] (\ |
| 548 | 0: 42 (), \ |
| 549 | 1: [object] (wow: cool (), ), \ |
| 550 | 2: [[]] (0: [] (), ), \ |
| 551 | ), \ |
| 552 | object: [object] (\ |
| 553 | a: [object] (\ |
| 554 | b: c (), \ |
| 555 | d: [e] (0: e (), ), \ |
| 556 | ), \ |
| 557 | ), \ |
| 558 | string: hi (), \ |
| 559 | )"; |
| 560 | |
| 561 | let rendered = reg.render("walk", &input).unwrap(); |
| 562 | assert_eq!(expected_output, rendered); |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn test_strict_each() { |
| 567 | let mut reg = Registry::new(); |
| 568 | |
| 569 | assert!(reg |
| 570 | .render_template("{{#each data}}{{/each}}", &json!({})) |
| 571 | .is_ok()); |
| 572 | assert!(reg |
| 573 | .render_template("{{#each data}}{{/each}}", &json!({"data": 24})) |
| 574 | .is_ok()); |
| 575 | |
| 576 | reg.set_strict_mode(true); |
| 577 | |
| 578 | assert!(reg |
| 579 | .render_template("{{#each data}}{{/each}}", &json!({})) |
| 580 | .is_err()); |
| 581 | assert!(reg |
| 582 | .render_template("{{#each data}}{{/each}}", &json!({"data": 24})) |
| 583 | .is_err()); |
| 584 | assert!(reg |
| 585 | .render_template("{{#each data}}{{else}}food{{/each}}", &json!({})) |
| 586 | .is_ok()); |
| 587 | assert!(reg |
| 588 | .render_template("{{#each data}}{{else}}food{{/each}}", &json!({"data": 24})) |
| 589 | .is_ok()); |
| 590 | } |
| 591 | |
| 592 | #[test] |
| 593 | fn newline_stripping_for_each() { |
| 594 | let reg = Registry::new(); |
| 595 | |
| 596 | let tpl = r#"<ul> |
| 597 | {{#each a}} |
| 598 | {{!-- comment --}} |
| 599 | <li>{{this}}</li> |
| 600 | {{/each}} |
| 601 | </ul>"#; |
| 602 | assert_eq!( |
| 603 | r#"<ul> |
| 604 | <li>0</li> |
| 605 | <li>1</li> |
| 606 | </ul>"#, |
| 607 | reg.render_template(tpl, &json!({"a": [0, 1]})).unwrap() |
| 608 | ); |
| 609 | } |
| 610 | } |