act_as_newbie

Keeping a newbie mindset, at all times.

ICanHaz Templates - Dealing With the Lack of Dot Notation

| Comments

[UPDATE: 30/10/2011] According to this issue we now have dot notation in moustache.js! Yay!

The ICanHaz javascript templating system is just awesome. It is really simple, and powerful, BUT as all good things in life it’s not perfect! ICanHaz is built on top of moustache.js that, on his part, is a javascript implementation of the moustache templates. Originally the moustache implementation didn’t support dot notation for accessing nested objects. Like for instance

1
2
3
4
5
6
7
8
    var post = {
      title : "Post title",
      body : "post body",
      author : {
        name : "Rafael Barbosa",
        age : 26
      }
    };

Normally you can access the author’s name in my js files via dot notation like post.author.name, but until this commit this wasn’t possible within moustache’s templates and it still isn’t supported by the javascript implementation. So how can I access nested objects? The same way you iterate over arrays!

For the post object above, inside an ICanHaz template

And that’ll do it. There’s an open issue on the moustache.js github for the inclusion of the dot notation.