Mustache.js: Accessing Array item by index in template

Created on 24 Jan 2012  ·  7Comments  ·  Source: janl/mustache.js

<li lat="{{ll[0]}}" lng="{{ll[1]}}">{{name}}</li>

ll is a two item array [47, -122]

Is this valid or do I have to write a function to extra the item at index I want?

Thanks!

Most helpful comment

Why can't mustache do this like a normal person and use [0] for indexing? This is really messy.

All 7 comments

{{ll.0}} and {{ll.1}} will work in most mustache implementations... haven't tried it in mustache.js though :)

Thanks, I'll give that a try.

Indeed, if you're using 0.5 (currently on master) you can do something like this:

mustache.render("{{a.0}}", {a: ['hi']})
=> 'hi'

this should be on the documentation, it's very usefull. You can even do this if you are int he current item: {{0}}

how can I use another {{variable}} as the index?
i.e. {{my_array.{{my_index}}}}

@piazera You can't. Mustache feels like that's one step too far over the "logic-less" line.

Why can't mustache do this like a normal person and use [0] for indexing? This is really messy.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

funston picture funston  ·  7Comments

MatthijsZw picture MatthijsZw  ·  18Comments

mbrodala picture mbrodala  ·  16Comments

Immortalin picture Immortalin  ·  12Comments

SmasherHell picture SmasherHell  ·  18Comments