ハンドルバーテンプレートを使用しています。dom要素を表示/非表示する必要があることに基づいてjsonに「displayType」というプロパティがあります。JSONレスポンスのタイプに基づいてHtml DOM要素を表示する方法HandleBars
いつ私のDOMのコメントを表示する必要があります。したがって、条件が適切に機能していないため、新しいことです。
JSONオブジェクト:レベル3のための
{
"menu": {
"level1": [
{
"title": "level1 title1",
"level2": [
{
"title": "level2 title 1",
"displayType": "txtimgdesc",
"level3": [
{
"title": "level3 title 1"
}
]
},
{
"title": "level2 title 2",
"displayType": "img",
"level3": [
{
"title": "level3 title 2"
}
]
},
{
"title": "level2 title 3",
"displayType": "textimg",
"level3": [
{
"title": "level3 title 3"
}
]
}
]
}
]
}
}
HTML:
<ul class="nav-list list-inline container">
{{#each ./level3}}
<li class="list-section">
<a href="#" class="">
<!-- display Image only if the type is txtimgdesc or txtimg or img -->
<img class="img-responsive {{showType ../displayType}}" src="">
<!-- display header title only if the type is txtimgdesc -->
<span class="{{showType ../displayType}}">Header title</span>
<!-- display title only if the type is txtimgdesc or txtimg -->
<span class="{{showType ../displayType}}">{{{title}}}</span>
</a>
</li>
{{/each}}
</ul>
登録ヘルパー:
Handlebars.registerHelper('showType', function(dType){
var _ckType = (dType) ? dType.toLowerCase(): "", returnType="";
returnType = (_ckType == "img") ? "" : "hidden";
returnType = (_ckType == "txtimgdesc") ? "" : "hidden";
returnType = (_ckType == "txtimg") ? "" : "hidden";
return returnType;
});
それはかなりきれいに見えます、私は今試してみましょう。おかげで多くの –
それは完全に動作します –