Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing Array item by index in template #158

Closed
rlightner opened this issue Jan 24, 2012 · 7 comments
Closed

Accessing Array item by index in template #158

rlightner opened this issue Jan 24, 2012 · 7 comments

Comments

@rlightner
Copy link

<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!

@bobthecow
Copy link

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

@rlightner
Copy link
Author

Thanks, I'll give that a try.

@mjackson
Copy link
Contributor

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

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

@ghost
Copy link

ghost commented May 9, 2012

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

@piazera
Copy link

piazera commented Dec 18, 2013

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

@bobthecow
Copy link

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

@JuergenSimon
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants