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

Can't access template's hash parameters' values from #each block within template #416

Closed
cryarchy opened this issue Feb 17, 2021 · 3 comments · Fixed by #419
Closed

Can't access template's hash parameters' values from #each block within template #416

cryarchy opened this issue Feb 17, 2021 · 3 comments · Fixed by #419
Assignees
Labels

Comments

@cryarchy
Copy link

I am not sure if this is supported by the handlebars spec or it is achievable in a different way. I am trying to access a variable passed in as a template's hash parameter within an #each block contained by the template. I can access the variable outside of the #each block but cannot seem to find a way to access it within the #each block.

The following code:

use serde_json;

use handlebars::Handlebars;

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let outer = r#"
    {{>inner name="fruit:" vegetables=fruits}}
  "#;

  let inner = r#"
    {{#each vegetables}}
      {{../name}} {{this}},
    {{/each}}
  "#;

  let mut handlebars = Handlebars::new();
  handlebars.register_template_string("outer", outer)?;
  handlebars.register_template_string("inner", inner)?;
  let data = serde_json::json!({ "fruits": vec!["carrot", "tomato"] });
  assert_eq!(
    handlebars
      .render("outer", &data)?
      .replace(" ", "")
      .replace("\n", ""),
    "fruit:carrot,fruit:tomato".to_owned()
  );
  Ok(())
}

fails with:

thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"carrot,tomato,"`,
 right: `"fruit:carrot,fruit:tomato"`', src/main.rs:20:3
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Should this be possible? Am I missing something?

@sunng87
Copy link
Owner

sunng87 commented Feb 18, 2021

Verified here that it works on handlebarsjs.

It seems the bug was related with the context of partial. Let me check .

@sunng87
Copy link
Owner

sunng87 commented Feb 20, 2021

@cryarchy this should be fixed in #419 . I will back port this fix to 3.x branch and create a release later today.

@cryarchy
Copy link
Author

cryarchy commented Feb 20, 2021

Awesome. Thank you @sunng87. I have confirmed that this works for my use case. :)

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

Successfully merging a pull request may close this issue.

2 participants