【问题标题】:Meteor: get template name in spacebarsMeteor:在空格键中获取模板名称
【发布时间】:2015-08-22 02:52:53
【问题描述】:

我开始开发一个流星网络应用程序。我将流星与铁路线和使用产量的主要 layout.html 文件一起使用。

在 router.js 我有:

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

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

在 layout.html 中:

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

我希望“主”标签具有实际加载模板的 ID,在这种情况下为“登录”。 有没有办法做到这一点?有什么建议吗? 谢谢

【问题讨论】:

    标签: javascript meteor iron-router spacebars


    【解决方案1】:

    我刚刚想通了

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

    【讨论】:

      【解决方案2】:

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

      templateonCreatedonRenderedonDestroyed 回调中是Template.instance()this

      【讨论】:

        【解决方案3】:

        您可以在布局上定义模板助手:

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

        【讨论】:

        • 刚试过,但返回总是未定义。可能是因为在加载的视图之外调用了助手?
        猜你喜欢
        • 2015-10-10
        • 2014-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-04
        • 1970-01-01
        • 1970-01-01
        • 2016-11-01
        相关资源
        最近更新 更多