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

How to use in the template has . variables #918

Open
YanHeDoki opened this issue May 19, 2024 · 0 comments
Open

How to use in the template has . variables #918

YanHeDoki opened this issue May 19, 2024 · 0 comments

Comments

@YanHeDoki
Copy link

YanHeDoki commented May 19, 2024

Like a structured context with an ip address as the key

use tera::{Context, Tera};

#[derive(Serialize)]
struct Device {
    pub device_ip: String,
    pub description: String,
}

fn main() {
    let raw_template = r#"
            info:{{10.0.0.1.description}}
        "#;

    let device = Device {
        device_ip: "10.0.0.1".into(),
        description: "Default device".into(),
    };

    let mut t = Tera::default();
    t.add_raw_template("template1", raw_template).unwrap();

    let mut context = Context::new();

    context.insert(&device.device_ip, &device);

    let res = t.render("template1", &context).unwrap();

    println!("{:?}", res)
}

burst out in error: `Error { kind: Msg("Failed to parse 'template1'"), source: Some(Error { kind: Msg(" --> 2:24\n  |\n2 |             info:{{10.0.0.1.description}}\n  |                        ^---\n 
 |\n  = expected `or`, `and`, `not`, `<=`, `>=`, `<`, `>`, `==`, `!=`, `+`, `-`, `*`, `/`, `%`, a filter, or a variable end (`}}`)"), source: None }) } `
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant