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

access array item by index #188

Closed
gibatronic opened this issue Jan 26, 2015 · 7 comments
Closed

access array item by index #188

gibatronic opened this issue Jan 26, 2015 · 7 comments

Comments

@gibatronic
Copy link

hi! is there a way to access an array item by its index position? this way it doesn't seems to work:

require 'mustache'

data = {
  collection: [{
    title: 'one'
  }, {
    title: 'two'
  }, {
    title: 'three'
  }]
}

template = '{{collection.0.title}}'

puts Mustache.render template, data

I expect one to be printed, but I only get an empty string.

@locks
Copy link
Member

locks commented Jan 26, 2015

Hello!
As far as I know that syntax is not valid per Mustache spec, so it is not implemented.

@bobthecow
Copy link
Member

@locks It's not invalid, either.

This is more of an "implementations should do what makes sense in the host language" sort of call. In PHP and JavaScript, this…

$collection[0]['title']

is how you would access the title property of the first value in an array called $collection. This…

$collection[0]['title']

… is how you'd access the title property of the value at the '0' key of a hash called $collection.

So in both of those languages, it would be surprising if you couldn't access array elements by index.

@locks
Copy link
Member

locks commented Jan 26, 2015

So that template works in mustache.php?

@bobthecow
Copy link
Member

Yep. And mustache.js

@locks
Copy link
Member

locks commented Jan 26, 2015

Hm. I'm not sure I like that, makes the behaviour variable from implementation to implementation.

@bobthecow
Copy link
Member

I wouldn't recommend it, since accessing things by array index feels like it violates the spirit of mustache to me. But it's possible :)

@gibatronic
Copy link
Author

hm... okay then! thanks guys! I'll parse it before hand and give everything ready to Mustache.

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

3 participants