# Check to see if template path exists?

**URL:** <https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081>\
**Category:** Ignition\
**Created:** [May 16, 2019, 6:52pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081 "2019-05-16T18:52:00Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [May 16, 2019, 6:52pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/1 "2019-05-16T18:52:00Z")

</div>

I’m trying to dynamically load templates via button press; I would like the button to show only if a template exists before loading it into a template holder

I’ve figured out that I can “load” a non-existent template into a hidden template holder, then run `.getLoadedTemplate()`, which will return `None` if it doesn’t exist… but that seems like an awfully hacky way around this problem

Any ideas?

Thanks!

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [May 19, 2019, 12:01am UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/2 "2019-05-19T00:01:26Z")

</div>

bump? Project due Monday

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [May 20, 2019, 6:03am UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/3 "2019-05-20T06:03:02Z")

</div>

This should get you started:

```python
p = event.source.getAppContext().getProject() #Get project reference from a vision component. 
_res = p.getResourcesOfType("fpmi","component-template") #Get a list of all templates
for res in _res: #Loop through resource list
	print p.getFolderPath(res.resourceId) #Print the path of all templates. You could match here or something. Maybe make a list when the window first opens, and compare "return "templatename" in listoftemplates

```

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [May 20, 2019, 2:48pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/4 "2019-05-20T14:48:57Z")

</div>

Yes! That is exactly what I need

I kind of forgot about getResourcesOfType and that. I made a [post](http://forum.inductiveautomation.com/t/programatically-search-through-templates/) where a few of the Ignition gurus helped me iterate through templates, that was back in October last year, and when I was still brand new to Ignition. Could have gotten it from there but I didn’t even think about that.

Thank you!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 20, 2019, 3:28pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/5 "2019-05-20T15:28:09Z")

</div>

Note that you’ll have to change that code completely once you upgrade to 8.0+ - the entire project interface changed to accommodate resources on the filesystem.

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [May 20, 2019, 3:30pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/6 "2019-05-20T15:30:22Z")

</div>

Thanks for the heads-up. Fortunately we have a staging environment and even if this functionality can’t be duplicated, it would be easy enough to create a fallback

But definitely something that needs to be considered

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [May 20, 2019, 4:23pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/7 "2019-05-20T16:23:35Z")

</div>

In my case I was using global templates, updated first line to

`p = event.source.getAppContext().getGlobalProject().getProject()`

big up

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [January 6, 2021, 7:36pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/8 "2021-01-06T19:36:55Z")

</div>

Here’s the updated version for 8.0:

```auto
from com.inductiveautomation.ignition.common.project.resource import ResourceType
from com.inductiveautomation.vision.api.client import VisionClientInterface

project = event.source.getAppContext().getProject() #Get project reference from a vision component. 
resource_type = ResourceType(VisionClientInterface.VISION_MODULE_ID, 'templates')
resources = project.getResourcesOfType(resource_type) #Get a list of all templates
paths = [str(resource.getResourcePath()) for resource in resources]

if 'com.inductiveautomation.vision/templates/foo' in paths:
    print 'template exists'

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 6, 2021, 9:51pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/9 "2021-01-06T21:51:48Z")

</div>

`com.inductiveautomation.factorypmi.application.model.TemplateManager#RESOURCE_TYPE` is the constant for the template resource type, if you want to use that instead of building your own.

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [January 6, 2021, 10:10pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/10 "2021-01-06T22:10:24Z")

</div>

Yeah that cleans it up a bit, thanks

```auto
def template_exists(template_name):
	"""
	Syntax: template_exists(template_name)

	Parameter:
	template_name: A string of the template's name. If in a directory prefix with the directory name: path/to/template

	Return Type: This method returns a Boolean value of class bool. This method returns True if the template exists otherwise returns False. 
	"""
	from com.inductiveautomation.factorypmi.application.model import TemplateManager
	
	context = event.source.getAppContext()
	project = context.getProject()
	resources = project.getResourcesOfType(TemplateManager(context).RESOURCE_TYPE)
	paths = [str(resource.getResourcePath()) for resource in resources]
	
	if 'com.inductiveautomation.vision/templates/' + template_name in paths:
		return True
	return False

print template_exists('foo')

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 6, 2021, 11:10pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/11 "2021-01-06T23:10:48Z")

</div>

As long as we’re golfing, this should also work and be marginally more efficient (short circuits, plus doesn’t build a list in memory):

```python
def template_exists(template_path):
	"""
	Syntax: template_exists(template_path)

	Parameter:
	template_path: A string of the template's name. If in a directory prefix with the directory name: path/to/template

	Return Type: This method returns a Boolean value of class bool. This method returns True if the template exists otherwise returns False. 
	"""
	from com.inductiveautomation.factorypmi.application.model import TemplateManager
	
	context = event.source.getAppContext()
	project = context.getProject()
	resources = project.getResourcesOfType(TemplateManager.RESOURCE_TYPE)
	return any(template_path in resource.folderPath for resource in resources)

print template_exists('foo')

```

---

<div class="post-metadata">

**Author:** ![chasondeshotel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/chasondeshotel/32/20032_2.png) [@chasondeshotel](https://forum.inductiveautomation.com/u/chasondeshotel)\
**Post date:** [January 6, 2021, 11:39pm UTC](https://forum.inductiveautomation.com/t/check-to-see-if-template-path-exists/25081/12 "2021-01-06T23:39:53Z")

</div>

You win; I’m handing over the solution point. Kudos on the use of `any`, every time I see that guy I remind myself I should be using it more

Nice improvements in readability and performance
