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

If lookup with parenthesis causes error #541

Closed
Nutomic opened this issue Oct 19, 2022 · 1 comment · Fixed by #543
Closed

If lookup with parenthesis causes error #541

Nutomic opened this issue Oct 19, 2022 · 1 comment · Fixed by #543

Comments

@Nutomic
Copy link

Nutomic commented Oct 19, 2022

I was surprised by the following code throwing an error:

let mut reg = Handlebars::new();
reg.set_strict_mode(true);

// register template using given name
reg.register_template_string("tpl_1", "hello {{#if (lookup my_vec 1)}}some{{else}}none{{/if}}")?;
let my_vec = vec![Some(1), None];
println!("{}", reg.render("tpl_1", &json!({"my_vec": my_vec}))?);
Ok(())

Error: RenderError { desc: "Value is missing in strict mode", template_name: Some("tpl_1"), line_no: Some(1), column_no: Some(7), cause: None, unimplemented: false }

There are two ways to fix it. Either disable strict mode, or replace template string with the following (note removed parenthesis inside if).

reg.register_template_string("tpl_1", "hello {{#if lookup my_vec 1}}some{{else}}none{{/if}}")?;

I would expect the original code to work, based on handlebars docs.

@sunng87
Copy link
Owner

sunng87 commented Oct 22, 2022

I believe {{#if lookup my_vec 1}} is not what you want because it checks if lookup is a valid value and ignores my_vec and 1.

The error is thrown by strict check because your second element in my_vec is None. But I agree that in this case, strict mode should not throw error. Let me improve this behaviour in our upcoming 5.0 beta releases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants