如何在meteor模板上使用条件?

如何在meteor模板上使用条件?,meteor,Meteor,我想在meteor模板上使用一个条件,如: 例如,我有一个字段名为type,type可以得到两个值1和2 我试着像下面这样,但它不起作用 {{#if type 1}} <td tableHeadData='day'> {{day}}</td> {{else type 2}} <td tableHeadData='month'> {{month}}</td> {{/if}} {{{#if type 1}} {

我想在meteor模板上使用一个条件,如: 例如,我有一个字段名为type,type可以得到两个值1和2

我试着像下面这样,但它不起作用

    {{#if type 1}}
    <td tableHeadData='day'> {{day}}</td>
    {{else type 2}}
    <td tableHeadData='month'> {{month}}</td>
    {{/if}}
{{{#if type 1}}
{{day}
{{else类型2}}
{{month}
{{/if}

答案是你不能。if的
参数必须是返回布尔值的模板辅助函数。因此,您必须编写一个助手函数来进行测试

Template.xxx.helpers({
  typeIs1() { return this.type === 1 }
  typeIs2() { return this.type === 2 }
})

答案是你不能。if
参数必须是返回布尔值的模板辅助函数。因此,您必须编写一个助手函数来进行测试

Template.xxx.helpers({
  typeIs1() { return this.type === 1 }
  typeIs2() { return this.type === 2 }
})

您可以制作一个
equals
helper以实现可重用性

Template.registerHelper('equals', function (a, b) {
  return a == b; // maybe triple equals of types need to match
});
然后在模板中使用如下内容:

{{#if equals value1 value2}}
  Your values are equal
{{/if}}

您可以制作一个
equals
helper以实现可重用性

Template.registerHelper('equals', function (a, b) {
  return a == b; // maybe triple equals of types need to match
});
然后在模板中使用如下内容:

{{#if equals value1 value2}}
  Your values are equal
{{/if}}

meteor中有一个名为raix:handlebar helpers的包,其中包含一些简单的helpers

Template.myTemplate.helpers({
    isEqual : function (value1, value2){
       return value1 === value2;
    }
})
因此,如果您想使用equal,那么您只需编写
{{{if$eq a b}}

{{#if $eq type 1}}
    <td tableHeadData='day'> {{day}}</td>
  {{else}} 
      {{#if $eq type 2}}
         <td tableHeadData='month'> {{month}}</td>
      {{/if}}
{{/if}}
对于全局,您必须使用Template.registerHelper注册助手

Template.registerHelper('isEqual', function (value1, value2) {
  return value1 === value2;
});
在这两种类型的帮助程序中,html代码都会喜欢

{{#if isEqual type 1}}
    <td tableHeadData='day'> {{day}}</td>
  {{else}} 
      {{#if isEqual type 2}}
         <td tableHeadData='month'> {{month}}</td>
      {{/if}}
{{/if}}
{{{#如果是相等类型1}
{{day}
{{else}
{{{#如果等位类型2}
{{month}
{{/if}
{{/if}

@注意:meteor spacebar目前不支持任何其他if条件。

meteor中有一个名为raix:handlebar helpers的包,其中包含一些简单的helpers

Template.myTemplate.helpers({
    isEqual : function (value1, value2){
       return value1 === value2;
    }
})
因此,如果您想使用equal,那么您只需编写
{{{if$eq a b}}

{{#if $eq type 1}}
    <td tableHeadData='day'> {{day}}</td>
  {{else}} 
      {{#if $eq type 2}}
         <td tableHeadData='month'> {{month}}</td>
      {{/if}}
{{/if}}
对于全局,您必须使用Template.registerHelper注册助手

Template.registerHelper('isEqual', function (value1, value2) {
  return value1 === value2;
});
在这两种类型的帮助程序中,html代码都会喜欢

{{#if isEqual type 1}}
    <td tableHeadData='day'> {{day}}</td>
  {{else}} 
      {{#if isEqual type 2}}
         <td tableHeadData='month'> {{month}}</td>
      {{/if}}
{{/if}}
{{{#如果是相等类型1}
{{day}
{{else}
{{{#如果等位类型2}
{{month}
{{/if}
{{/if}
@注意:meteor spacebar目前不支持任何其他if条件