sunng87 / handlebars-rust

Rust templating with Handlebars
MIT License
1.26k stars 137 forks source link

Why my code fails to use outer scope for each #667

Closed gintsgints closed 1 month ago

gintsgints commented 1 month ago

https://github.com/gintsgints/handlebars-rust/blob/master/tests/each.rs:

extern crate handlebars;
#[macro_use]
extern crate serde_json;

use handlebars::Handlebars;

#[test]
fn test_subexpression() {
    let hbs = Handlebars::new();

    let data = json!({"a": 1, "b": [{"n" : "n1"}, {"n": "n2"}]});

    assert_eq!(
        hbs.render_template("{{a}} {{#each b as |ar|}}{{a}}{{ar.n}} {{/each}}", &data)
            .unwrap(),
        "1 1n1 1n2 "
    );
}

Why this test fails with rendering "1 n1 n2" instead? Should not {{a}} be recognized inside each block?

gintsgints commented 1 month ago

Sorry, I found a solution. I had to use {{../a}} for outer context.