Diving into nested templates and repeaters, is this right?

Continuing my journey into nested templates, origin point is this thread, but I did change the structure a little. My intent was to make it simpler, but I'm not sure if this is the right way to do it.

BTW: If you have suggestions on how I could create better posts with Vision content, I am most welcome to feedback. In Perspective I would copy the JSON, but in Vision the object is a long XML string and difficult to read.

This is the lowest level template:
image

I have three of these inside another template with an additional button:
image

This template, then, is used inside a Template Repeater, currently three repeated instances, with a total of 19 instances.

Note: I did post pictures like these in my previous thread, but I have slightly different question this time with a slightly different structure.

With this code, I have found a way to read from the UDT on the lowest level template and write to it's components. (I think I got the Java or Python(?) class descriptors from a post by @justinedwards.jle )

repeater = event.source.parent.getComponent('Template Repeater 2')
templateList = repeater.getLoadedTemplates()
for template in templateList:
	for counter in range(3):
		for component in template.components:
		if 'TemplateHolder' in component.__class__.__name__:
			compDir = component
			print compDir.name # the name of each instance of the Recipe_Manager_Crystal_1
			if compDir.UDT.Control.ON_OFF == False:
				tempComp = compDir.getComponent(0) # The name of the template Recipe_Manager_Crystal_1					
				numObj = tempComp.getComponent(5)
				numObj.value = 10.0

Note: Don't mind the naming convention of some of those variables, I just reused them for different types/objects as I was going.

The end goal, here is to bring in a dataset of values to write to the Numeric Text Field (which is referenced by this: numObj = tempComp.getComponent(5), then allow the user to adjust said value using the +/- buttons, and finally write those values to each UDTs Control Value for the selected positions.

One thing I am not sure about is how to count the number of templates within another template, hence the counter in range(3). We will have templates with from two to four nested templates, so if there is a way to count the number of nested templates, I would love to learn it!

To re-posit the question: Is this code representing a good/right way of reading UDTs and writing to components in those lowest level nested templates, and the second part will this work to write to the UDTs? (Using writeBlocking(), of course.)

Vision is perfectly happy to let you drill up or down in any component hierarchy--there's no barriers to any introspection.

As for lengths--any variable-size content in any Vision component is driven by some kind of dataset, which has a .rowCount property, or directly by a repeat count. No need to use constants. And for cases where you want to iterate through a list (of templates perhaps), you can use python's enumerate() function to supply integers with the objects.

2 Likes