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?

ptamzz
  • 9,235
  • 31
  • 91
  • 147

2 Answers2

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}}
ptamzz
  • 9,235
  • 31
  • 91
  • 147
  • 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. – OneWorld Jul 31 '12 at 09:02
  • i cant get the "dot notation" working for a simple object. =( eg. card { id:10, name: "maxwell" } –  May 27 '13 at 15:43
  • Doesn't this depend on the actual processor? – Joshua Ramirez Jun 12 '13 at 19:46
  • Check your version number. http://stackoverflow.com/questions/9347998/is-it-bad-practice-to-use-dot-notation-in-mustache-php – d1val Jun 24 '13 at 13:26
6

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

d1val
  • 401
  • 4
  • 4