How to access Template Canvas property in instance

I am using a template canvas with multiple template instances in it. I would like to access the properties of this template canvas on click of button inside the instance. Currently, I am using following code to access the template canvas and then its properties:

templateCanvas = system.gui.getParentWindow(event).getRootContainer().getComponent(templateCanvasName)
property = templateCanvas.propertyName

while it is working fine, it will break if the template canvas is moved inside another container. To circumvent this issue, passing the absolute path of template canvas to template instances through templates dataset property is one of the options I can think of.

But is there any other way to get the reference of template canvas without using the name? something similar to event.source.parent.parent?

Exactly that, though you'll have to experiment to get the right number of .parent references (depends on canvas vs. repeater vs. single instance holder).

Using event.source.parent on template instance, I can access all properties of the instance in template canvas. However interesting thing is, event.source.parent.parent.name gives None, event.source.parent.parent.x or y returns 0 but event.source.parent.parent.width or height gives me correct values of template canvas width/height. Also, using event.source.parent.parent to access custom properties or other properties like templates of template canvas throws error.
I am curious which component it is referencing by event.source.parent.parent? and how this hierarchy works?

Try print type(event.source.parent.parent) (and so on) if you’re curious about the internals.
Basically, when actually embedded in a window, templates have to be ‘embedded’ inside of a (normally transparent to the designer) TemplateHolder object. There’s complicated reasons for this, but we generally try to hide it from you; except when you’re doing scripting like this, where we really can’t. As Phil mentioned, when using the template repeater or canvas you’ll even need additional .parent calls, due to the way those components have to do their own layout.