当字段为空时如何省略一行(使用胡须/把手)

时间:2015-03-02 04:53:27

标签: javascript handlebars.js mustache

我目前正在使用小胡子并尝试实施一些基本的if-else检查。就我而言,我正在检查字段中的值。

例如:

我有这个JSON数据:

"AGENCIES":[
    {
        "IASTATE":"Buenos Aires",
        "IAADDRESS1":"Paraguay 647 Piso 4 Of. 17\/18",
        "IAADDRESS3":"",
        "IALEGALNAME":"Silvia Stocker Australia & New Zealand Travel (ANZ Group)",
        "IACOUNTRY":"Argentina",
        "IAPRINCIPALAGENT":"",
        "IAADDRESS2":"",
        "IAAGENCY":"Australia & New Zealand Group (ANZ Group)",
        "IAEMAIL":"info@anzgroup.com.ar",
        "IAPHONE":"+54 11 4311 9828",
        "IAWEBSITE":"http:\/\/www.anzgroup.com.ar\/education-105",
        "IACITY":"Buenos Aires",
        "IALOCATIONHEAD":"Cecilia Minzio"
    },
    {
        "IAAGENCY":"CW International Education",
        "IAADDRESS1":"Juan Francisco Segui 3967",
        "IAPRINCIPALAGENT":"",
        "IALOCATIONHEAD":"Carola Wober",
        "IACOUNTRY":"Argentina",
        "IACITY":"Buenos Aires",
        "IAPHONE":5.41148010867E11,
        "IAADDRESS2":"Piso 6A",
        "IAWEBSITE":"http:\/\/www.cwinternationaleducation.com",
        "IASTATE":"Buenos Aires",
        "IALEGALNAME":"CW International Education",
        "IAEMAIL":"info@cwinternationaleducation.com",
        "IAADDRESS3":"Ciduad Autonoma"
    }]

从上面的数据可以看出,某些字段可能像地址2一样空,但有些数据总是有代理商名称,国家或网站等值。

那么当字段为空时,如何省略整个“行”?类似的东西:

<h3>{{IAAGENCY}}<h3>
{{if IAADDRESS2 is not empty}}<span>{{IAADDRESS2}}</span><br />{{/if}}
<span>{{IACOUNTRY}}</span><br />
<span>{{IAWEBSITE}}</span>

由于

1 个答案:

答案 0 :(得分:3)

使用handlebars,您只需检查所述json值的长度:

<h3>{{IAAGENCY}}<h3>
{{#if IAADDRESS2.length}}<span>{{IAADDRESS2}}</span><br>{{/if}}
<span>{{IACOUNTRY}}</span><br />
<span>{{IAWEBSITE}}</span>

如果长度为0,则评估为false,因此省略该行