 getTemplateInstance = (view) ->
-  while view and not view.template
+  while view and not view._templateInstance
     view = view.originalParentView or view.parentView
 
-  # Body view has template field, but not templateInstance, so we have ? at the end.
-  view?.templateInstance?()
+  view?._templateInstance
 
 # More or less the same as aldeed:template-extension's template.get('component') just specialized.
 # It allows us to not have a dependency on template-extension package and that we can work with Iron

  # Template.dynamic should use "this.template" in its templates, but it does not, so we have a special case here for it.
  return null if template.viewName in ['Template.__dynamicWithDataContext', 'Template.__dynamic']

  # TODO: Blaze.View::lookup should not introduce any reactive dependencies. Can we simply ignore reactivity here? Can this template instance or parent template instances change without reconstructing the component as well? I don't think so. Only data context is changing and this is why templateInstance() inside templateInstanceToComponent is reactive and we do not care about data context here.
  component = Tracker.nonreactive ->
    templateInstanceToComponent templateInstance


    throw new Error "Expected DOM element." unless domElement.nodeType is Node.ELEMENT_NODE

    templateInstanceToComponent =>
      Blaze.getView(domElement)?._templateInstance

  mixins: ->
    []
