【问题标题】:How to retrieve Blaze template name from inside?如何从内部检索 Blaze 模板名称?
【发布时间】:2019-11-03 06:06:42
【问题描述】:

我想从内部使用流星模板的名称:

<template name="blaModal">
    <div class="modal fade" id="{{templateName}}"> 
    </div>
</template>

我该怎么做?

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案1】:

    您可以创建一个全局帮助器,它解析当前模板的实例并返回它的视图名称(去掉Template. 前缀):

    /imports/startup/client/helpers.js

    Template.registerHelper('templateName', function () {
      const instance = Template.instance()
      const { view } = instance
      const { name } = view
      return name.replace('Template.', '')
    })
    

    【讨论】:

    • 真正的问题是你为什么需要这样做?我觉得你好像在绕圈子来完成一件非常简单的事情。
    • 有了这个,您可以轻松地为每个模板生成 i18n 模式,例如为每个当前路线显示标题。
    猜你喜欢
    • 2019-02-02
    • 2021-02-19
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多