Skip to main content

template_rendering_with_options/
template_rendering_with_options.rs

1use std::collections::HashMap;
2use std::error::Error;
3use webots_proto_ast::Proto;
4use webots_proto_template::{RenderContext, RenderOptions, RenderWebotsVersion, TemplateField};
5
6fn main() -> Result<(), Box<dyn Error>> {
7    let input = r#"#VRML_SIM R2025a utf8
8
9PROTO ContextAwareBox [
10  field SFInt32 count 2
11  field SFString label "default-label"
12]
13{
14  Group {
15    children [
16      WorldInfo { title "%<= fields.label.value >%|%<= fields.label.defaultValue >%|%<= context.os >%" }
17      %< for (let i = 0; i < fields.count.value; ++i) { >%
18        Transform {
19          translation %<= i >% 0 0
20          children [ Box { size 0.5 0.5 0.5 } ]
21        }
22      %< } >%
23    ]
24  }
25}
26"#;
27
28    let document: Proto = input.parse()?;
29
30    let mut field_overrides = HashMap::new();
31    field_overrides.insert("count".to_string(), TemplateField::SFInt32(3));
32    field_overrides.insert(
33        "label".to_string(),
34        TemplateField::SFString("override-label".to_string()),
35    );
36
37    let context = RenderContext::default()
38        .with_os("linux")
39        .with_world("/workspace/worlds/example.wbt")
40        .with_webots_version(RenderWebotsVersion::new(
41            "R2025a".to_string(),
42            "0".to_string(),
43        ));
44
45    let options = RenderOptions::default()
46        .with_field_overrides(field_overrides)
47        .with_context(context);
48
49    let rendered = webots_proto_template::render(&document, &options)?;
50    println!("{rendered}");
51
52    Ok(())
53}