Check if component is a template

What would be a good way of checking if a component is a template?

I am running into a problem where I am looping through all my components and calling getComponents() and then checking the length of the results returned. if the length is greater than 0 i call getComponents() again. the prolblem is that when i call getComponets on a template the results has a length of 1, but i cannot perform any actions on the components inside a template.

[code]item = event.source.parent.getComponent(‘MainContainerTopBanner’)


if(item is not a template):<— need code to check if item is a template
components = item.getComponents()

if(len(components) > 0):
	for component in components:

It’s a little hacky, but you can extract the class name of a component by copying the component into a text editor, and looking at the tag in the xml code. You can then use getClass() and endswith() to see if the string ends with a specific type:

if not(str(item.getClass()).endswith("TemplateHolder'>")):

There’s a couple of other ways you could do it, such as using pythons find() function for strings, but this looks at the end of the string, where the type of the component is guaranteed to be.

I was also looking for a way to loop through the components in a container and find which are templates. I’d like to contribute my solution. I think my solution is rather hacky too (probably even more so than James’!)

templateList = []
for comp in event.source.parent.getComponents():
		thisTemplatePath = comp.templatePath
		print "Component " + + " is not a template!"

This is roughly the code I wrote for a function to create a list of templates. I used this code along with some other functions to automatically rename templates based on a UDT member string. I am only using this function during development because renaming every template instance is a very tedious and error prone task. It’s worth noting that I ended up going with the solution James posted.

Now if I could only figure out a way to modify a template property binding in a script…