在Meteor中使用Spacebars逻辑而不遵守逻辑

时间:2016-06-14 17:15:54

标签: meteor spacebars

我有一个模板帮助器,它返回一个Session的值,在这种情况下,它返回数字1:

Template.Student.helpers({
  curWeek: function () {
    return Session.get('CurrentWeek').substr(0, 1);
},

我的模板有一个表,我正在尝试根据辅助函数的值打印到表的一部分。所以我在模板中有一些逻辑来打印正确的部分。但它不遵守逻辑。即使curWeek的值返回值1,模板也会在{{#if curWeek 2}}下运行逻辑,因此两者都在表中。我只希望{{#if curWeek 1}}下的部分运行,因为这就是价值所在。我没有正确使用逻辑吗?

<template name="Student">
  {{#modalizeBody}}
  <table class="bordered narrow">
    <thead>
      <tr>
        <th>Name</th>
        <th>Shift</th>
        <th>Age</th>
        <th>Sex</th>
        <th>Level</th>
        <th>Sun</th>
        <th>Mon</th>
        <th>Tue</th>
        <th>Wed</th>
        <th>Thu</th>
        <th>Fri</th>
        <th>Sat</th>
      </tr>
    </thead>
  <tbody>
  {{#each StudsWk1Master}}
    {{#if curWeek 1}}
      <tr>
        <td>{{FullName}}</td>
        <td>{{RoomWk1}}</td>
        <td>{{calculateAge Bdate}}</td>
        <td>{{Sex}}</td>
        <td>{{Level}}</td>
        <td>{{formatName this.Teachers.Week1.Sunday}}</td>
        <td>{{formatName this.Teachers.Week1.Monday}}</td>
        <td>{{formatName this.Teachers.Week1.Tuesday}}</td>
        <td>{{formatName this.Teachers.Week1.Wednesday}}</td>
        <td>{{formatName this.Teachers.Week1.Thursday}}</td>
        <td>{{formatName this.Teachers.Week1.Friday}}</td>
        <td>{{formatName this.Teachers.Week1.Saturday}}</td>
      </tr>
    {{/if}}
  {{/each}}
  {{#each StudsWk1Master}}
    {{#if curWeek 2}}
      <tr>
        <td>{{FullName}}</td>
        <td>{{RoomWk2}}</td>
        <td>{{calculateAge Bdate}}</td>
        <td>{{Sex}}</td>
        <td>{{Level}}</td>
        <td>{{formatName this.Teachers.Week2.Sunday}}</td>
        <td>{{formatName this.Teachers.Week2.Monday}}</td>
        <td>{{formatName this.Teachers.Week2.Tuesday}}</td>
        <td>{{formatName this.Teachers.Week2.Wednesday}}</td>
        <td>{{formatName this.Teachers.Week2.Thursday}}</td>
        <td>{{formatName this.Teachers.Week2.Friday}}</td>
        <td>{{formatName this.Teachers.Week2.Saturday}}</td>
      </tr>
    {{/if}}
  {{/each}}
  </tbody>
 </table>
{{/modalizeBody}}
</template>

1 个答案:

答案 0 :(得分:1)

你的帮助者没有测试是否平等。你有:

{{#if curWeek 1}}

但是你的帮助者只返回当前的一周并且不期望参数。

只需将参数添加到辅助函数中,然后返回一个布尔值:

Template.Student.helpers({
  curWeek: function (value) {
    return Session.get('CurrentWeek').substr(0, 1) === value;
},