Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make #each handle non-iterable values like the original JavaScript version #380

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 50 additions & 5 deletions src/helpers/helper_each.rs
Expand Up @@ -136,16 +136,12 @@ impl HelperDef for EachHelper {
rc.pop_block();
Ok(())
}
(false, _) => {
_ => {
if let Some(else_template) = h.inverse() {
else_template.render(r, ctx, rc, out)?;
}
Ok(())
}
_ => Err(RenderError::new(format!(
"Param type is not iterable: {:?}",
value.value()
))),
},
None => Ok(()),
}
Expand Down Expand Up @@ -479,4 +475,53 @@ mod test {
let rendered = reg.render_template(template, &input).unwrap();
assert_eq!("01", rendered);
}

#[test]
fn test_non_iterable() {
let reg = Registry::new();
let template = "{{#each this}}each block{{else}}else block{{/each}}";
let input = json!("strings aren't iterable");
let rendered = reg.render_template(template, &input).unwrap();
assert_eq!("else block", rendered);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#[test]
fn test_recursion() {
let mut reg = Registry::new();
assert!(reg
.register_template_string(
"walk",
"(\
{{#each this}}\
{{#if @key}}{{@key}}{{else}}{{@index}}{{/if}}: \
{{this}} \
{{> walk this}}, \
{{/each}}\
)",
)
.is_ok());

let input = json!({
"array": [42, {"wow": "cool"}, [[]]],
"object": { "a": { "b": "c", "d": ["e"] } },
"string": "hi"
});
let expected_output = "(\
array: [42, [object], [[], ], ] (\
0: 42 (), \
1: [object] (wow: cool (), ), \
2: [[], ] (0: [] (), ), \
), \
object: [object] (\
a: [object] (\
b: c (), \
d: [e, ] (0: e (), ), \
), \
), \
string: hi (), \
)";

let rendered = reg.render("walk", &input).unwrap();
assert_eq!(expected_output, rendered);
}
Copy link
Contributor Author

@mkantor mkantor Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compatibility check.

The output of this one differs slightly, but only because handlebars-rust stringifies objects and arrays in its own way (FWIW I like the handlebars-rust stringifications better).

Copy link
Contributor Author

@mkantor mkantor Sep 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recursion was my main motivation behind this pull request, by the way. I couldn't recursively iterate through data structures because handlebars-rust would error on the first non-iterable value it encountered.

}