akdubya / dustjs

Asynchronous templates for the browser and node.js
http://akdubya.github.com/dustjs/
MIT License
1.44k stars 124 forks source link

Access nth element of an array #18

Open aslakhellesoy opened 13 years ago

aslakhellesoy commented 13 years ago

I couldn't find a mailing list, so here goes:

I'm trying to access only the first element of an array in my template:

{#people.0}
  Hello, {name}
{/people.0}

However, that gives me a syntax error. Is it possible to access only the first element, or do I need to write a function for this?

Thanks for a great lib!

aslakhellesoy commented 13 years ago

I found a workaround. My template:

{#nth o="names" n="1"}{title} {name}{~n}{/nth}

My context:

{
  "title": "Sir",
  "names": [{
    "name": "Moe"
  },
  {
    "name": "Larry"
  },
  {
    "name": "Curly"
  }],
  nth: function(chunk, context, bodies, params) {
    return context.current()[params.o][params.n];
  }
}

Result:

Sir Larry

It would be nice to be able to just do this though:

{#names[1]}}{title} {name}{~n}{/names}

Would you consider adding this (or something similar) to dust?

Baggz commented 12 years ago

+1

wraithmonster commented 12 years ago

Is this still the only way to access an array item?

jairodemorais commented 12 years ago

hi guys, we are maintaining and improving dust for Linkedin and we have mantained the repo as opensource. Now I have implemented what you were talking in this thread and it will be ready as soon as this pull request is merged. if you want, take a look! and anything you need just tell me. https://github.com/linkedin/dustjs/pull/33 Jairo

wraithmonster commented 12 years ago

Fantastic!

vybs commented 12 years ago

it is ready now !