How to access array element by index in mustache in template node

I have a simple flow that generates xml file using a template. But I have troubles using the data from the payload that holds the nested properties, as if only the first level of nesting is supported.
The flow is below, the atrtibute "value" of the tag is empty in the resulting XML file. Am I missing something here?
image

[{"id":"10864cf4.ba412b","type":"inject","z":"e4c94bd5.d53538","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":460,"wires":[["491ab592.6ffc24"]]},{"id":"491ab592.6ffc24","type":"function","z":"e4c94bd5.d53538","name":"build data","func":"msg.array = [{text: \"111 111 111 111\", template: { \n                                                    valueName : \"BreakingNews\",\n                                                    fields: [\"BreakingText\"]\n                                                  }\n            },\n            {text: \"222 222 222\", template: { \n                                                valueName : \"BreakingNews\",\n                                                fields: [\"BreakingText\"]\n                                            }\n            }\n];\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":460,"wires":[["fd6e7856.7c4f2","f6110285.4a7b18"]]},{"id":"fd6e7856.7c4f2","type":"template","z":"e4c94bd5.d53538","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<?xml version=\"1.0\"?>\n<tickerfeed version=\"2.4\">\n  <playlist name=\"BreakingNews_______\" target=\"carousel\" type=\"flipping_carousel\">\n    <group>\n      <description>Breaking News</description>\n      <elements>\n        {{#array}}\n        <element>\n          <template>{{{template.valueName}}}</template>\n          <field name=\"{{{template.fields[0]}}}\">{{{text}}}</field>\n        </element>\n        {{/array}}\n      </elements>\n    </group>\n  </playlist>\n</tickerfeed>","output":"str","x":460,"y":460,"wires":[["77413eda.bd5358"]]},{"id":"77413eda.bd5358","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":600,"y":460,"wires":[]},{"id":"f6110285.4a7b18","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":450,"y":400,"wires":[]}]
1 Like

Start in reverse.

Take the final expected XML through and XML node and see how it creates a JS object. Then do the reverse.

Well, the flow like below:


produces the right xml, but this is not the case here, let's skip the xml, simplified flow that shows the issue below, just the nested array value (template.fields[0]) is empty when used in template.

Data:

{
	"text": "sure",
	"template": {
		"fields": ["some name"]
	}
}

Template:

This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}}!

[{"id":"a6885cfc.a7045","type":"inject","z":"e4c94bd5.d53538","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":660,"wires":[["80cdd649.aa6a18"]]},{"id":"80cdd649.aa6a18","type":"change","z":"e4c94bd5.d53538","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \t\"text\": \"sure\", \t\"template\": { \t\t\"fields\": [\"some name\"] \t} }","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":660,"wires":[["3befd0f8.c7f39"]]},{"id":"3befd0f8.c7f39","type":"template","z":"e4c94bd5.d53538","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}}!","output":"str","x":480,"y":660,"wires":[["77d87954.9800a8"]]},{"id":"77d87954.9800a8","type":"debug","z":"e4c94bd5.d53538","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":640,"y":660,"wires":[ ]}]

use .0 instead of [0]

[{"id":"12a4c5dc.63ca2a","type":"template","z":"dadb596f.70e948","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"This works: {{{payload.text}}}, and this not: {{{payload.template.fields[0]}}} but this does... {{{payload.template.fields.0}}} !","output":"str","x":600,"y":100,"wires":[["cad92c2c.cb817"]]}]
This works: {{{payload.text}}}, and this too: {{{payload.template.fields.0}}}!
5 Likes

Indeed, it works, thank you very much!

1 Like

no worries.

I updated the title to assist future users (who face the same issue) find your post.

2 Likes

Indeed, it now describes the issue correctly, thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.