Center and space items within template repeater based on number of templates within repeater

I am using a template repeater and I want to change the number of template instances within the repeater based on a tag. Is there a way to automatically center and space each template within the repeater? I see the horizontal gap parameter but outside of putting a binding on that property based on the number of templates I cant see a way to evenly space the templates within the repeater.

Im using vision, any ideas on that?

Sorry, I didn't notice the tag. I'll delete my answer.
I don't know how to do it in Vision.

The template repeater doesn't really support this kind of customization.

The template canvas sort of does, in that miglayout is inherently more flexible, though you'll need to know or determine ahead of time how many templates to put on each row. Once you've done that, you could use the spanx layout keyword, for one possibility, to make a template consume more than the fixed number of columns.

Thanks for the suggestion though!

Yes doesnt seem like it. If I had more time I would look into this, seems pretty cool.

What kind of tag is this? Dataset?

My initial idea would be to take advantage of the absolute positioning option of the template canvas and simply script the positioning using a property change event.

Example:
Take note of the parameters that are needed for rendering the specific template:

Position the template canvas in the window, right click on the canvas, and select Customizers-->Custom Properties from the popup

Add a property called rawParameters:
image

Bind the custom property to the parameters tag:
image

Right click on the template canvas again and select scripting:

Inside the component scripting window, select the propertyChange event handler:

Develop a script that processes the data into a proper dataset for the templates property of the canvas and includes x coordinates that center as well as evenly space the templates:

# When the raw parameters are updated,
# ...process them into proper template canvas parameters
if event.propertyName == 'rawParameters':
	
	# Assign the new parameters to a variable with human readable name
	rawParameters = event.newValue
	
	# Get the column headers from the template parameters for the new parameters dataset
	# [name, template, parameters, x, y, width, height, layout, z]
	headers = system.dataset.getColumnHeaders(event.source.templates)
	data = [] # Empty list for processed parameter row data
	
	# Assumes the templates will be square
	# Assumes a horizontal strip display where the template will take the full height of the canvas
	templateHeight = templateWidth = event.source.height
	
	# Calculate the total white space and divide it evenly between the sides and templates
	# Add 1 to the row count divisor, eliminates the possibility of dividing by zero
	# ...and acounts for the right side
	centeringValue = (event.source.width - (templateWidth * rawParameters.rowCount)) / (1 + rawParameters.rowCount)
	
	# Iterate through each row of the dataset and process the parameters
	for row in xrange(rawParameters.rowCount):
		motorNumber, status, speed = [rawParameters.getValueAt(row, column) for column in xrange(rawParameters.columnCount)]
		data.append(['Motor {}'.format(motorNumber), # Arbitrarily create an identifier for the specific template
			'TestTemplates/Motor', # The Vision template path for the template to be painted
			unicode({"motorNumber":motorNumber,"status":status,"speed":speed}), # Create a string that represents a dictionary of parameters
			centeringValue + (centeringValue * row) + (templateWidth * row), # The 1st side + all subseqent gaps and templates
			0, # Position the y coordinate at the very top of the canvas
			templateHeight,
			templateWidth,
			'n/a', # A custom layout is not needed for absolute positioning
			row]) # Arbitrarily assign the row index to the z parameter
	
	# Set the processed data to the templates dataset parameter of the canvas
	event.source.templates = system.dataset.toDataSet(headers, data)

Result with two templates in the dataset tag:
image

Result with three templates in the dataset tag:


Result with only one template in the dataset tag:
image

Edit: Removed some overlooked debugging artifacts from the test script

2 Likes

Wow...looks like it would work! I'm sure I can use this in the future...thanks for the help!