# Add template dynamically

**URL:** <https://forum.inductiveautomation.com/t/add-template-dynamically/8377>\
**Category:** Ignition\
**Created:** [April 28, 2014, 11:23am UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377 "2014-04-28T11:23:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![raelix](https://avatars.discourse-cdn.com/v4/letter/r/ecd19e/32.png) [@raelix](https://forum.inductiveautomation.com/u/raelix)\
**Post date:** [April 28, 2014, 11:23am UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/1 "2014-04-28T11:23:24Z")

</div>

Hi everybody,  
I’m trying to dynamically add a template but without being able to change the size and content,  
code is:

```auto

from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder

template = TemplateHolder()
template.setName("test")
template.visible = True
template.setTemplatePath("ItemPopup/GENERIC_POPUP") #here can't load right popup
template.setSize(400,400) #here can't load right size
root = system.gui.getParentWindow(event).rootContainer
root.addComponent(template)
```

---

<div class="post-metadata">

**Author:** ![marioquark](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@marioquark](https://forum.inductiveautomation.com/u/marioquark)\
**Post date:** [April 29, 2014, 4:43pm UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/2 "2014-04-29T16:43:00Z")

</div>

`from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder
root = system.gui.getParentWindow(event).rootContainer
newobj = TemplateHolder()
newobj.initTemplate(root.getAppContext())
newobj.setTemplatePath("XXX")
newobj.setName("DynTemplate")
newobj.getLoadedTemplate().setEnableLayout(1)
root.addComponent(newobj)`  
 ![8)](https://us1.discourse-cdn.com/inductiveautomation/original/1X/587d7fe0d6822f36e81815ab06aa7164e9d2e057.gif "Cool")

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [April 29, 2014, 5:22pm UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/3 "2014-04-29T17:22:26Z")

</div>

After marioquark’s code is written you can resize and move the template instance around with the system.gui.moveComponent and system.gui.resizeComponent functions.

Example:

```auto
from com.inductiveautomation.factorypmi.application.components.template import TemplateHolder
root = system.gui.getParentWindow(event).rootContainer
newobj = TemplateHolder()
newobj.initTemplate(root.getAppContext())
newobj.setTemplatePath("test1")
newobj.setName("DynTemplate")
newobj.getLoadedTemplate().setEnableLayout(1)
root.addComponent(newobj)
system.gui.moveComponent(newobj, 100,100)
system.gui.resizeComponent(newobj, 400,400)
```

Unsupported by IA, but powerful and awesome code. I will always remember marioquark as wearing cool sunglasses.

Nick Mudge  
Ignition Consultant  
[nickmudge.info](http://nickmudge.info)

---

<div class="post-metadata">

**Author:** ![marioquark](https://avatars.discourse-cdn.com/v4/letter/m/22d042/32.png) [@marioquark](https://forum.inductiveautomation.com/u/marioquark)\
**Post date:** [April 30, 2014, 7:45am UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/4 "2014-04-30T07:45:31Z")

</div>

😆

---

<div class="post-metadata">

**Author:** ![Vita](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@Vita](https://forum.inductiveautomation.com/u/Vita)\
**Post date:** [April 20, 2015, 11:29am UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/5 "2015-04-20T11:29:24Z")

</div>

Hi,

I have only a small addition. If you want to use the dynamics within the template (animations, bindings, …) template should be initialized with one extra line:

```auto
newobj.getLoadedTemplate().startup()
```

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [April 20, 2015, 5:09pm UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/6 "2015-04-20T17:09:08Z")

</div>

Vita, this is great to know. How did you find this out?

---

<div class="post-metadata">

**Author:** ![Vita](https://avatars.discourse-cdn.com/v4/letter/v/51bf81/32.png) [@Vita](https://forum.inductiveautomation.com/u/Vita)\
**Post date:** [April 22, 2015, 8:13am UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/7 "2015-04-22T08:13:34Z")

</div>

Hi Nick,

I needed set a variable in my dynamically added template and it did not work. I try simple animation using through tag and it did not work. I realized that some init is missing. So because of poor programmer manual in Ignition I tried to find a solution myself (through class analyzing). Then I remembered that I saw the init function list in trace log when error occurred. And indeed there was “VisionTemplate.initialize” line 🙂

So little reverse engineering and luck ![8)](https://us1.discourse-cdn.com/inductiveautomation/original/1X/587d7fe0d6822f36e81815ab06aa7164e9d2e057.gif "Cool")

---

<div class="post-metadata">

**Author:** ![nmudge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nmudge/32/3394_2.png) [@nmudge](https://forum.inductiveautomation.com/u/nmudge)\
**Post date:** [April 22, 2015, 2:00pm UTC](https://forum.inductiveautomation.com/t/add-template-dynamically/8377/8 "2015-04-22T14:00:12Z")

</div>

Gotcha, thanks!
