检查流星模板中的空值

时间:2012-07-20 13:31:56

标签: html templates meteor

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

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

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

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

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

然后,您可以在模板中将其用作if的参数:

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

答案 2 :(得分:2)

我认为你可以试试这个,你可以使用count如果值birthday eql 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