Meteor: get template name in spacebars

1.2k views Asked by At

I started to develop a meteor webapp. I use meteor with iron routes and a main layout.html file that use yield.

In router.js i have:

Router.configure({
  layoutTemplate: 'layout',
  loadingTemplate: 'loading'
 });

Router.route('/login', {name: 'login'});

In layout.html:

<template name="layout">
    <div class="container">
       {{> header}}
        <main id="{{ actualLoadedTemplate }}">
            {{> yield}}
        </main>
    </div>
</template>

I would that the 'main' tag, have the id of the actual loaded template, "login" in this case. Is there a way to accomplish that? Any advice? Thanks

3

There are 3 answers

1
saimeunt On

You can define a template helper on your layout :

Template.layout.helpers({
  actualLoadedTemplate: function(){
    return Router.current() && Router.current().template;
  }
});
0
Piero Maltese On

I just figured it out

Template.registerHelper('actualLoadedTemplate', function(){
    return Router.current() && Router.current().route.getName().replace('.','-');
  }
);
0
sunstory On

templateName = template.view.name.replace('Template.', '')

template is Template.instance() or this in onCreated, onRendered, onDestroyed callbacks