【问题标题】:Checking null value in meteor template检查流星模板中的空值
【发布时间】:2023-03-14 05:26:01
【问题描述】:

我正在使用流星。我有一个看起来像这样的模板,

<template name="SearchAct">
{{#each SearchPerson}}
    <div class="result"><!--This is one search result box-->
        <div class="resultContent">
            <img src={{payload.pic_square}} alt="profile photo" class="floatLeft" />
            <p>{{payload.uid}}</p>
            <span class="floatLeft">
                {{payload.first_name}}
                <br/>
                {{payload.last_name}}
            </span>
            <input type="checkbox" class="floatRight" />
            <h4>Tennis</h4>
            <span class="age_location">
                {{#if payload.birthday}}
                    {{payload.birthday}},
                {{/if}}
                {{#if payload.sex}}
                    {{payload.sex}}
                {{/if}}
                <br/>
                {{#if payload.hometown_location}}
                    {{payload.hometown_location.city}},
                    {{payload.hometown_location.state}},
                    {{payload.hometown_location.country}}
                {{/if}}
            </span>
            <div class="line"></div>
            <a href="#" class="clear" onclick="renderProfile({{payload.uid}});">See Their Details</a>  
        </div><!-- End of resultContent--> 
    </div><!-- End of result box-->
{{/each}}
</template>

现在我想检查{{payload.birthday}} 的空值。在这里,如果我得到null 值,我想显示一条消息。如何检查 null 值?

【问题讨论】:

    标签: html templates meteor


    【解决方案1】:

    我想你只需要一个{{else}}

    <span class="age_location">
        {{#if payload.birthday}}
            {{payload.birthday}}
        {{else}}
            No birthday found
        {{/if}}
    </span>
    

    【讨论】:

    • 不确定这是否是一个问题,但简单地使用{{else}} 只会检查一个值是否为“假”;所以payload.birthday 可能是false"" 或其他不评估为真的值,{{else}} 将被评估。如果您想明确检查 payload.birthday 是否为空,而不是其他“虚假”值,您需要使用模板函数或 Handlebars 助手。
    【解决方案2】:

    Handlebars(以及 Meteor 的扩展)不允许模板内的逻辑。因此,您需要使用 Handlebars 助手扩展您的模板或所有项目的模板。将以下内容放入项目中任何客户端加载的 JavaScript 文件中:

    Handlebars.registerHelper("isNull", function(value) {
      return value === null;
    });
    

    然后你可以在你的模板中使用它作为if的参数:

    {{#if isNull payload.birthday}}Your birthday is null!{{/if}}
    

    【讨论】:

      【解决方案3】:

      我想你可以试试这个,如果值birthday eql 0 为假,你可以使用count,这对我有用

      <span class="age_location">
          {{#if payload.birthday.count}}
              {{payload.birthday}}
          {{else}}
              you display message
          {{/if}}
      </span>
      

      参考:https://groups.google.com/forum/#!topic/meteor-talk/Gumkz9VnLYY

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-21
        • 2018-12-04
        • 2016-10-24
        • 1970-01-01
        • 2012-12-15
        • 2015-11-18
        • 2015-06-11
        • 1970-01-01
        相关资源
        最近更新 更多