How to reference a specific object in an array of objects using jTemplates

Posted by Travis on Stack Overflow See other posts from Stack Overflow or by Travis
Published on 2010-04-05T08:54:09Z Indexed on 2010/04/05 9:03 UTC
Read the original article Hit count: 312

I am using the excellent jTemplates plugin to generate content.

Given a data object like this...

var data = {
 name: 'datatable',
 table: [
  {id: 1, name: 'Anne'},
  {id: 2, name: 'Amelie'},
  {id: 3, name: 'Polly'},
  {id: 4, name: 'Alice'},
  {id: 5, name: 'Martha'}
 ]
};

..I'm wondering if it is possible to directly specify an object in an array of objects using $T. (I'm hoping there is something like $T.table:3 available)

Currently the only way I can think of to access a specific object in an array is to do something like this...

{#foreach $T.table as record}

    {#if $T.record$iteration == 3}
        This is record 3!  Name:  {$T.record.name}
    {#/if}

{#/for}

However that seems clumsy...

Any suggestions?

Thanks

© Stack Overflow or respective owner

Related posts about jtemplate

Related posts about JavaScript