【问题标题】:Meteor: Handlebars able to combine #if and #with?Meteor:车把可以结合#if 和#with 吗?
【发布时间】:2016-06-18 20:32:14
【问题描述】:

目前,我正在从第三方数据库中提取信息,并且模板使用该数据。

<template name="dogTypeData">
  {{#if userOwnsDog typeOfDog}}
    {{#with userOwnsDog typeOfDog}}
      // populate info here with user's dog
    {{/with}}
  {{else}}
    You do not own this kind of dog yet!
  {{/else}}
  {{/if}}
</template>

助手查看主人是否拥有这种狗:

Template.dogTypeData.helpers({
  userOwnsDog: function(typeOfDog){
    return OwnedDogs.findOne({
      type: typeOfDog
    });
  }
});

我很好奇是否有办法结合#if#with 语句,或者是否有必要这样做。为简化代码,只需调用一次userOwnsDog(typeOfDog) 而不是两次。比如,有没有{{#ifwith}} 类型的语句?

谢谢!

【问题讨论】:

    标签: meteor meteor-blaze meteor-helper


    【解决方案1】:

    您可以将它们组合起来,因为这里不需要初始的if。同样的模板可以这样重写:

    <template name="dogTypeData">
      {{#with userOwnsDog typeOfDog}}
        // populate info here with user's dog
      {{else}}
        You do not own this kind of dog yet!
      {{/with}}
    </template>
    

    另见this section 来自Understanding Spacebars

    【讨论】:

    • 哇哦,我没有意识到#with 有一个内置的else!看起来它对我有用。谢谢,大卫!
    • 是的,空格键充满了这样的技巧。我很高兴你能成功。
    猜你喜欢
    • 2013-11-21
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2020-01-17
    • 2020-03-29
    • 2021-07-01
    • 2013-03-26
    • 2012-07-08
    相关资源
    最近更新 更多