Get data context from a template instance

I have been searching on the Internet and couldn’t find anything. Is there an elegant way to find a Template instance and get it’s data context outside of that template? I am currently doing that, this event listener is on my body wrapper:

'click [data-ion-radio]': function(event) {
     // Get template name for Blaze.render
     var templateName = $(event.currentTarget).data('ion-radio');

     // Get the rest of attributes from the button
     var data = $(event.currentTarget).data();
     // This is my array of objects that I need to pass as data, I am currently
        passing id of a wrapper of my template that holds the required data
     var = Blaze.getView($('#'+data.template)[0]).templateInstance();, data);

**** data.template = 'wrapper-id' as per sample template below ****

So this forces me to the following template syntax for the template with data

<template name="myTemplate">
    <div id="wrapper-id">

Any other way to get that data without having to wrap my template content? If I call Blaze.getView on event.currentTarget I have to get to the parent, but the number of levels I have to go up will differ throughout the app.


Just a quick answer. Sorry I didn’t have time to read more.

Have you tried the template parameter in the event handeler?

'click #shareScreen': function(e, template) {

Can’t tell you how many times I’ve had to look this up. It’s not in the meteor docs for some reason.

I think: Template.instance() may be an alternative.

Good luck!

Thanks for feedback! The problem is that the template passed onto my event handler is the main parent (body element). It is not the template that contains my data, thus I would still have to look for the child template by name.

Well, the current data context in event helper should be “this” by default.
Or check the todo example, how it is used there
And check it in browser console.

If it does not work, you can still do someting like

good luck

Unfortunately ‘this’ is not the template I am looking for. I would hope there was a simple Blaze API that allows Template instance access. What is in my primary post is the only thing that have worked for my use case, however I would want to avoid an extra wrapper in my template just for the sake of this working.

But data context or template instance scope? These are 2 quite different things.
You want to access something created in for example onCreated?

Or the Spacebars result feeded to it by some helper ?

You can access scope etc from all the parent templates using for example

Still if you better describe what you are trying to code, we will 99% find better solution how to do it.
Not just that template up there, but what u want to accomplish from user story perspective.

Thanks a lot, template.get() and template.parent() were exactly what I was looking for. I was already using kind of a parent template loop, but haven’t thought of skipping (contentBlock) templates like this one does.


This works perfectly thanks to the above package.

And what I was trying to achieve - I needed a modal with selectable list items generated from the data of current active template. However, my event handler is global, the list ‘modal’ will be called in variety of places including other modals and templates. My primary obstacle was that when trying to access parent templates of ‘event.currentTarget’ I would get (contentBlock) template, and the number of levels to go up to get the data would differ from template to template.


If a second argument is passed (<templateName>, <dataContext>)), it will be used as the data context for the template (works as the data parameter for Blaze.renderWithData).

So I would just use this modal extension and pass it as data object which would 1 property for example options: ourArrayOfOptions

So u would be able to reuse that modal with xyz different options, combinations and list of options

Thanks, already figured it out with the package you posted. Using Meteoric Ionic, but it looks like both modal implementations work the same way.

Though low level API with direct access to a Template instance would be really great instead of having to loop through parents in search for template instance with specified field (via Blaze.TemplateInstance.prototype.get from that package).

nothing is stopping u from assigning link to any template instance to some global variable or so…

I agree, also a valid solution, although I wanted to avoid global variables and having to assign the link from every template. Thanks again for pointing me in the right direction