如何在字段为空时省略一行(使用 mustache/handlebars)

how do I omit a line when field is empty (using mustache/handlebars)

我目前正在使用 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"
    }]

正如您从上面的数据中看到的,有些字段可以为空,例如 address2,但有些数据将始终具有值,例如机构名称或国家/地区或网站。

so 如何在字段为空时省略整个 "line"?类似于:

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

谢谢

使用 handlebars 你可以只检查所述 json 值的长度:

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

如果长度为 0,则计算结果为 false,因此省略该行