简体   繁体   中英

Meteor: get template name in spacebars

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

I just figured it out

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

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

templateTemplate.instance()thisonCreatedonRenderedonDestroyed回调

You can define a template helper on your layout :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM