how to access nested objects with mustache js templating engine
Asked Answered
T

2

27

I've this json return

{
    "timeline": [{
        "id": "2",
        "self": {
            "uid": "2",
            "username": "ptamzz"
        },
        "file": {
            "fid": "43",
            "file_name": "First Name"
        },
        "connection": {
            "fid": "4",
            "username": "tom"
        },
        "action": "viewed your document",
        "time": "2012-01-16 12:23:03",
        "tags": ["Engineering", "Computer Science", "Java", "Java Library"]
    }, {
        "id": "1",
        "self": {
            "uid": "2",
            "username": "ptamzz"
        },
        "file": {
            "fid": "41",
            "file_name": "Write Up"
        },
        "connection": {
            "fid": "4",
            "username": "tom"
        },
        "action": "favorited your document",
        "time": "2012-01-16 12:22:04",
        "tags": ["Design"]
    }]
}

According to the tutorial at http://coenraets.org/blog/2011/12/tutorial-html-templates-with-mustache-js/ (Sample 6: Nested Object section), you can access dot notation to access the nested objects.

From the above json, I want to retrieve the data like self.username, file.file_name etc etc.

Now, I've my template as

{{#timeline}}
    <li>
        {{self.username}}
    </li>
{{/timeline}}

But self.username doesn't work.

How do I retrieve these nested values?

Twig answered 18/1, 2012 at 15:16 Comment(0)
T
28

I don't think it's the right way to do but since I couldn't find any answers here, I figured out something myself. At least this works.

{{#timeline}}
    <li>
        {{#self}}{{username}}{{/self}}
    </li>
{{/timeline}}
Twig answered 21/1, 2012 at 13:11 Comment(4)
dot notation DOES work. There must be an error in your special case. Maybe because "self.username" is in an array and can appear several times.Medulla
i cant get the "dot notation" working for a simple object. =( eg. card { id:10, name: "maxwell" }Blodget
Doesn't this depend on the actual processor?Tuberculin
Check your version number. #9348498Kayo
K
6

Dot notation does not work on version 0.4x and below. It worked on "0.7.2".

Kayo answered 24/6, 2013 at 15:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.