user1184100
user1184100

Reputation: 6894

Generate DOM content only for specific key's in JSON using handlebar

I'm using handlebar js for templating. If i want the handlebar to generate dom content only for keys 2,3 or 1,2,3 from the json data how can i do it ?

JSON structure

{
"items": {
    "1": {
        "title": "Title1",
        "date": "2/16/2012"
    },
    "2": {
        "title": "Title2",
        "date": "2/16/2012"
    },
    "3": {
        "title": "Title3",
        "date": "2/16/2012"
    },
    "4": {
        "title": "Title4",
        "date": "2/16/2012"
    }
}
}

HTML should have data as shown below

<li> data of item2 </li>
<li> data of item3 </li>

Any help ?

Upvotes: 0

Views: 130

Answers (1)

AdityaParab
AdityaParab

Reputation: 7100

For your problem, this

var DOMCreationKeys = [2,3];
var k=0;

var updatedJSON = {
"items": {}};

for (item in myjson_data.items){
    if(item == DOMCreationKeys [k++]){
        updatedJSON["items"].push(myjson_data.items[item]);
}

contnr.append(list_tmpl(updatedJSON ));

Should work :)

Upvotes: 1

Related Questions