Reputation: 1
I am trying out Pebble templating for a config file which is in YAML format. The value for this comes from another YAML file. That is -
server:
-
box1:
host:"12345"
server:
-
env: "abc"
host: {{ server.box1.host }}
I could read the config.yml as string and create a Pebble template. Using evaluate I am able to replace the template with context variable whose name is not nested (example {{ serverBox1Host }} --> this works).
If I use {{ server.box1.host }}, I get com.mitchellbosecke.pebble.error.RootAttributeNotFoundException: Root attribute [server] does not exist or can not be accessed and strict variables is set to true. If I set strictVariables as false, the {{ server.box1.host }} is replaced with empty.
How to solve this?
context.put("server.box1.host", "suriya" );
I took references from: https://www.programcreek.com/java-api-examples/?api=com.mitchellbosecke.pebble.PebbleEngine
I am using "String Template":
new PebbleEngine.Builder()
.strictVariables(true)
.newLineTrimming(false)
.loader(new StringLoader())
.build();
Thanks
Upvotes: 0
Views: 1159
Reputation: 1
Resolved this:
Variable name assigned in the context was server like - context.put("server", new HashMap<String, Object> map1);
And map1 had nested map whose keys was box1 and host. The value of the host was suriya.
map1 :
key - box1
value - Map with key host and value suriya.
In the Pebble template was able to access {{ server.box1.host }} and this rendered with value "suriya"
Upvotes: 0
Reputation: 116
You can use the dot (.) notation to access attributes of variables that are objects. If the attribute contains any atypical characters, you can use the subscript notation ([]) instead.
{{ foo.bar }}
{{ foo["bar"] }}
Behind the scenes foo.bar will attempt the following techniques to to access the bar attribute of the foo variable:
If foo is a Map,
foo.get("bar")
foo.getBar()
foo.isBar()
foo.hasBar()
foo.bar()
foo.bar
Additionally, if foo is a List, then foo[0] can be used instead of foo.get(0).
You can take a look at pebble documentation here : https://pebbletemplates.io/wiki/guide/basic-usage/
Upvotes: 0