【问题标题】:Using different variables with the same name in different templates在不同的模板中使用同名的不同变量
【发布时间】:2016-04-14 20:03:14
【问题描述】:

我有两个模板,它们包含在一些布局模板中:

{{>b_afisha_today}}
{{>b_afisha_soon}}

我想在我的一个模板的帮助器中使用一些变量。

Template.b_afisha_today.onCreated(function() {
    this.data.day = new Date().getDate();
}

Template.b_afisha_today.helpers({
    times: function() {
        var day = Template.instance().data.day;
    }
})

这里的问题是 Template.instance().data.day 现在属于父布局模板(我猜也属于全局范围)。因此,如果我在第二个模板中初始化一个具有相同名称的新变量,它将改变我的第一个模板中的所有内容。

Template.b_afisha_soon.onCreated(function() {
    this.data.day = 'mess everything';
}

不确定是否可以使用两个同名的独立变量,每个变量都可以在一个模板中访问?

【问题讨论】:

    标签: meteor


    【解决方案1】:

    您可以像这样将数据从父模板传递到子模板。

    <div class="slot-wrapper">
      {{> b_afisha_today day=day}}
      {{> b_afisha_soon day=day}}
      ...
    </div>
    
    <template name="b_afisha_today">
      <div class="day"><span>{{day}}</span></div>
    </template>
    
    <template name="b_afisha_soon">
      <div class="day"><span>{{day}}</span></div>
    </template>
    

    希望能解决你的问题。

    【讨论】:

    • 不幸的是它不能。有不同的父模板。我只是不能(也不想)生成数据并将其从父母那里传递给我的模板。我认为这应该是在模板中限定变量范围的某种方式。
    猜你喜欢
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2020-10-20
    • 2014-08-06
    • 2022-01-19
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多