How do i get template name..?

hello,

suppose i have following template:

<template name="template1">
  {{> someTemplate}}
</template>

<template name="template2">
  {{> someTemplate}}
</template>

<template name="template3">
  {{> someTemplate}}
</template>

how do i get template name for following purpose:

Template.someTemplate.events({
	'click': function () {
		if (isTemplateName() === 'template1') {
			// do something
		}
		if (isTemplateName() === 'template2') {
			// do something
		}
		if (isTemplateName() === 'template3') {
			// do something
		}
	}
});

is there built in function for isTemplateName() …?? or any way to create isTemplateName() function…??

thank You so much,

This would not work, because someTemplate must be equal to either template1, template2 or template3. Maybe you assumed that a template file is a template. But this is not the case. A template is always created by one single <template> tag, so you actually have 3 templates here, plus an event helper for a fourth template called someTemplate.

EDIT: Ok, I think I got what you are trying to achieve. Instead of checking the template name, you can pass additional data to someTemplate like this:

<template name="template1">
  {{> someTemplate comingFrom='template1'}}
</template>

You can then access this property in your event handler:

Template.someTemplate.events({
	'click': function (event, instance) {
		if (instance.data.comingFrom === 'template1') {
                ...
                }
	}
});

Instead of comingFrom I would use something like parent in practice, but I wanted to make sure that you don’t think this is a built-in keyword.

1 Like

hello @waldgeist thanks for your response… what i mean is template name,

woooah, i didn’t know that before, care to elaborate a little…??

The syntax is described here, although I have to admit it’s a bit hard to read:
https://github.com/meteor/meteor/blob/devel/packages/spacebars/README.md

1 Like

I would also recommend you to have a look at this package:

This allows you to get properties defined in the JS code of parent templates (which is probably not what you wanted here, but related).

1 Like

your code works…!! thank You so much Sir… i learn a lot new things…

You’re welcome. I know how it is to start with Meteor :slight_smile:

1 Like

The template has a view structure which has the name

Template.events({
 'click .button': function(e, instance) {
  console.log(instance.view.name);
 }
});
Template.helpers({
 helper: function() {
  var instance = Template.instance();
  return instance.view.name;
 }
});
2 Likes

Did not know that, may come in handy in certain cases. But it would not help here, I suppose, since @karina wanted the name of the parent template. Is it possible to get this somehow, too?

1 Like