Pregunta

On the handlebars website I can see how to loop through an array but I'm trying to look through an array object and having no luck.

Here's my data, which I am getting back using jquery and a jsonp command.

[
    {
        "id": 95,
        "user_id": 6,
        "status": 1,
        "name": "Lost & Co",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "London",
        "venue": "Lost & Co",
        "fsqid": "512fb191e4b0f660fda426eb",
        "address": "160 Putney High Street",
        "postcode": null,
        "lat": "51.461322924772",
        "lng": "-0.21689196269354",
        "phone": "+442087802235",
        "description": null,
        "date": "2014-05-10",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-24 12:20:09",
        "updated_at": "2014-04-24 12:20:09"
    },
    {
        "id": 96,
        "user_id": 6,
        "status": 1,
        "name": "Lost & Co",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "London",
        "venue": "Lost & Co",
        "fsqid": "512fb191e4b0f660fda426eb",
        "address": "160 Putney High Street",
        "postcode": null,
        "lat": "51.461322924772",
        "lng": "-0.21689196269354",
        "phone": "+442087802235",
        "description": null,
        "date": "2014-05-24",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-24 12:20:57",
        "updated_at": "2014-04-24 12:20:57"
    },
    {
        "id": 94,
        "user_id": 6,
        "status": 1,
        "name": "Lost Society",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "London",
        "venue": "Lost Society",
        "fsqid": "4bbcf543593fef3b35de0256",
        "address": "697 Wandsworth Rd",
        "postcode": "SW8 3JF",
        "lat": "51.467991033249",
        "lng": "-0.14425992965698",
        "phone": "+442076526526",
        "description": null,
        "date": "2014-05-31",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "http://www.lostsociety.co.uk/index2.html",
        "twitter": "lostsocietybar",
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-24 12:18:53",
        "updated_at": "2014-04-24 12:18:53"
    },
    {
        "id": 97,
        "user_id": 6,
        "status": 1,
        "name": "Campus North",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "Newcastle",
        "venue": "Campus North",
        "fsqid": "533b05b0498ea89103652747",
        "address": "5 Carliol Square",
        "postcode": null,
        "lat": "54.973037719727",
        "lng": "-1.6087431907654",
        "phone": null,
        "description": null,
        "date": "2014-06-12",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-24 17:56:26",
        "updated_at": "2014-04-24 17:55:55"
    },
    {
        "id": 91,
        "user_id": 6,
        "status": 1,
        "name": "Glastonbury",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "Pilton",
        "venue": "Glastonbury",
        "fsqid": null,
        "address": null,
        "postcode": null,
        "lat": null,
        "lng": null,
        "phone": null,
        "description": null,
        "date": "2014-06-28",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "http://www.glastonburyfestivals.co.uk/",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-22 12:08:32",
        "updated_at": "2014-04-22 13:06:51"
    },
    {
        "id": 92,
        "user_id": 6,
        "status": 1,
        "name": "Glastonbury",
        "country": {
            "code": "GB",
            "name": "United Kingdom"
        },
        "city": "Pilton",
        "venue": "Glastonbury",
        "fsqid": null,
        "address": null,
        "postcode": null,
        "lat": null,
        "lng": null,
        "phone": null,
        "description": null,
        "date": "2014-06-29",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "http://www.glastonburyfestivals.co.uk/",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-04-22 12:08:46",
        "updated_at": "2014-04-22 13:07:27"
    },
    {
        "id": 80,
        "user_id": 6,
        "status": 1,
        "name": "TBA",
        "country": {
            "code": "ES",
            "name": "Spain"
        },
        "city": "Ibiza",
        "venue": "TBA",
        "fsqid": "51fab9b1498e0ced82ae61d0",
        "address": "Mediterranean Sea",
        "postcode": null,
        "lat": "38.970107",
        "lng": "1.305714",
        "phone": null,
        "description": null,
        "date": "2014-09-12",
        "time": "",
        "image": null,
        "image_thumb": null,
        "website": "",
        "twitter": null,
        "tickets": "",
        "fb_event": null,
        "created_at": "2014-02-27 00:41:49",
        "updated_at": "2014-02-27 00:41:49"
    }
]

I was trying this but it's not working. Have I missed something obvious?

    <ul>
        {{#each}}
        <li>
            <h3>{{ venue }}</h3>
        </li>
        {{/each}}
    </ul>
¿Fue útil?

Solución

You need to provide a context to that #each:

{{#each .}}
    <li>
        <h3>{{ venue }}</h3>
    </li>
{{/each}}

All you're missing is that you need to let handlebars know that it's the root object as such that you want to iterate over.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top