# Get Template Root from Component Inside the Template

**URL:** <https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504>\
**Category:** Ignition\
**Created:** [November 10, 2020, 8:02pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504 "2020-11-10T20:02:50Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [November 10, 2020, 8:02pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/1 "2020-11-10T20:02:50Z")

</div>

Is there any way via scripting to directly get a handle to the parent template “root container” from a component inside the template? I’m trying to create a script that can handle being fired from a variable number of nested containers/groups inside a template. I don’t want to rely on a relative path since it needs to be updated whenever you create a new nested container between the component firing the script and the root template. My current solution is to loop through “parents” until I find a type of ‘VisionTemplate’ which works fine but messy. This feels like a situation where there is a built in method/function that I’m missing…

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 10, 2020, 8:14pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/2 "2020-11-10T20:14:44Z")

</div>

Your solution is exactly what I do, interested like you though if there’s a cleaner one!

---

<div class="post-metadata">

**Author:** ![bschroeder](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bschroeder/32/13260_2.png) [@bschroeder](https://forum.inductiveautomation.com/u/bschroeder)\
**Post date:** [November 10, 2020, 9:39pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/3 "2020-11-10T21:39:20Z")

</div>

If it is some event that is happening you can use:

```auto
rootContainer = system.gui.getParentWindow(event).getRootContainer()

```

See: [https://docs.inductiveautomation.com/display/DOC81/system.gui.getParentWindow](https://docs.inductiveautomation.com/display/DOC81/system.gui.getParentWindow)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [November 10, 2020, 9:49pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/4 "2020-11-10T21:49:23Z")

</div>

You want SwingUtilities, in a script module like so:

```auto
from javax.swing import SwingUtilities
from com.inductiveautomation.factorypmi.application.components.template import VisionTemplate

def getTemplateRoot(component):
    return SwingUtilities.getAncestorOfClass(VisionTemplate, component)

```

Then you can call it anywhere with:

```auto
someModule.getTemplateRoot(event.source)

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [November 11, 2020, 12:48am UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/5 "2020-11-11T00:48:24Z")

</div>

This doesn’t work for templates as it gets the Window’s rootContainer object, not the Template’s rootContainer

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [November 11, 2020, 4:37pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/6 "2020-11-11T16:37:58Z")

</div>

This is great, thanks Phil!

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [December 9, 2020, 12:16am UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/7 "2020-12-09T00:16:54Z")

</div>

Is it possible to access custom methods on objects returned via this function? I’ve tried a few different flavors but always get an AttributeError. I’ve confirmed the custom method works when called via `event.source.parent.<customMethodName>()` and I also confirmed they are both pointing to the same object (same type and same name value).

I created another version of your script above that looks to see if the template is contained within a TemplateRepeater and it works great to find the repeater. This repeater has a custom method to refresh some custom props. I want to call this method from within the template, but currently the handle returned to the repeater object doesn’t work with calling the custom method for some reason (but calling `repeater_handle.name` returns the proper name just fine…). Only way I can currently call the custom method from within the template is with a million parents (`event.source.parent.parent.parent.parent.parent.customMethod()`). Hoping there is a way to get this to work with the handle returned from the function…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [December 9, 2020, 1:10am UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/8 "2020-12-09T01:10:11Z")

</div>

Well, yes, with some help. Java Swing methods that work on this level are unaware of the special behavior of Vision Components. If you install my Simulation Aids module, it installs a generic fix for this in the Jython interpreter. And then your component methods and custom properties will work in jython no matter how you get a hold of the object. (:

If you want your brain fried, look at this topic (start here but go back through the whole thing for context):

> [@Transaction groups triggered expression item updating when trigger is false](http://forum.inductiveautomation.com/t/transaction-groups-triggered-expression-item-updating-when-trigger-is-false/17196/15):
>
> Thank you very much for the quick fix. Been testing this new beta on a few use cases and haven’t issued any gotchas yet slight_smile

---

<div class="post-metadata">

**Author:** ![WillMT10](https://avatars.discourse-cdn.com/v4/letter/w/7feea3/32.png) [@WillMT10](https://forum.inductiveautomation.com/u/WillMT10)\
**Post date:** [December 9, 2020, 5:22pm UTC](https://forum.inductiveautomation.com/t/get-template-root-from-component-inside-the-template/40504/9 "2020-12-09T17:22:53Z")

</div>

Interesting. You weren’t kidding, that topic is quite the rabbit hole… I’ll check out your Simulation Aids module. Thanks Phil!
