5

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

<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值?

4

3 回答 3

4

我认为你只需要一个{{else}}

<span class="age_location">
    {{#if payload.birthday}}
        {{payload.birthday}}
    {{else}}
        No birthday found
    {{/if}}
</span>
于 2012-07-21T08:53:21.760 回答
3

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

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

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

{{#if isNull payload.birthday}}Your birthday is null!{{/if}}
于 2013-07-15T00:03:55.693 回答
2

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

<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

于 2015-11-25T08:49:52.120 回答