njuptxiaot / json-template

Automatically exported from code.google.com/p/json-template
0 stars 0 forks source link

Provide @index variable for the current array index #28

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
This is really common and trivial to implement.

Then you can to {section $index odd?} color1 {.or} color2 {.end}

Or also {$index|even? color1 color2}

Could make it 'index', but that might clobber people's variables.  $ means
a "special" variable.

Original issue reported on code.google.com by gtempacc...@yahoo.com on 22 Jul 2009 at 1:47

GoogleCodeExporter commented 8 years ago
Done in the Python version:

http://code.google.com/p/json-template/source/detail?r=286

Original comment by gtempacc...@yahoo.com on 26 Sep 2009 at 8:48

GoogleCodeExporter commented 8 years ago
Done in JS and PHP too

Original comment by gtempacc...@yahoo.com on 7 Nov 2009 at 9:16

GoogleCodeExporter commented 8 years ago

Original comment by gtempacc...@yahoo.com on 7 Nov 2009 at 9:16

GoogleCodeExporter commented 8 years ago

Original comment by andyc...@gmail.com on 9 Nov 2009 at 6:58

GoogleCodeExporter commented 8 years ago
Assume:

{
  "url-base": "http://example.com/music/", 
  "playlist-name": "Epic Playlist", 
  "songs": [
    {
      "url": "1.mp3", 
      "artist": "Grayceon", 
      "bandmembers": [
    {
      "name": "Joe Singer",
          "intrument": "vocals"
    },
    {   
      "name": "Joe Drummer",
      "intrument": "drums"
    }
      ],
      "title": "Sounds Like Thunder"
    }, 
    {
      "url": "2.mp3", 
      "artist": "Thou",  
      "bandmembers": [
    {
      "name": "Joe Singer",
          "intrument": "vocals"
    },
    {   
      "name": "Joe Drummer",
      "intrument": "drums"
    }
      ],
      "title": "Their Hooves Carve Craters in the Earth"
    }
  ]
}

and

var t = jsontemplate.Template("
{.section songs}\n  
  <h2>Songs in '{playlist-name}'</h2>\n\n
  <table width=\"100%\">\n
    {.repeated section @}\n
      <tr>\n
        <td><a href=\"{url-base|htmltag}{url|htmltag}\">Play</a>\n
        <td><i>{title}</i></td>\n
        {.section bandMembers\n  
          <td>{artist}<br />\n
            <table width=\"100%\">\n
            {.repeated section @}\n
              <tr>\n
                <td>{$index}</td>\n
                <td><i>{intrument}</i></td>\n
                <td>{name}</td>\n
              </tr>\n
            {.end}\n
            </table>\n
          </td>\n
        {.end}\n
      </tr>\n
    {.end}\n 
  </table>\n
{.or}\n
  <p><em>(No page content matches)</em></p>\n
{.end}\n");

Will $index return (1, 2) or (5, 6, 10, 11)? I mean is it an index for the 
current 
array or is it a gloabl index like $pos?

Is ther a $pos? I downloaded the latest for JavaScript and tested, got nothing 
but 
undefined errors.

Original comment by jfur...@wal-mart.com on 9 Nov 2009 at 9:47

GoogleCodeExporter commented 8 years ago
@index should return the topmost/innermost index thing.  Each repeated section 
starts
@index counting from 1.

I'm not sure where the 5/6 10,11 come from in your example.  The behavior is 
verified
in this test case.  
http://chubot.org/json-template/test-cases/testTwoIndices-01.html

There is no @pos.  If you mean @index then it might not be released yet.  I'll 
do a
release now since the code is stable.

Original comment by gtempacc...@yahoo.com on 10 Nov 2009 at 2:41

GoogleCodeExporter commented 8 years ago
Yeah, that doesn't work. Error: exception thrown and not caught, debuger drops 
me at 
throw {
          name: 'UndefinedVariable', message: name + ' is not defined'
        }

Original comment by jfur...@wal-mart.com on 10 Nov 2009 at 2:54

GoogleCodeExporter commented 8 years ago
I just a released a new one a few minutes ago.  It should work as its verified 
by
tests.  Note it's @index, not $index.  This is parallel with @ for the cursor.

Original comment by gtempacc...@yahoo.com on 10 Nov 2009 at 2:55

GoogleCodeExporter commented 8 years ago
Update worked right out of the box, thanks!!

Original comment by jfur...@wal-mart.com on 10 Nov 2009 at 3:01

GoogleCodeExporter commented 8 years ago
Hey, I just released an update of json-template.js with incompatible API 
changes.

I doubt that you have used the APIs since they were not documented very well, 
but I
would recommend updating.

Sorry if this caused any inconvenience, but I wanted to rename the methods to 
comply
with common JavaScript style.  I released 4 days ago without realizing that I 
hadn't
done this yet.

It will affect you if you wrote formatters/predicates that accept a context

context.Lookup() --> context.get()

Or if you wrote any custom FunctionRegistry objects:

registry.Lookup --> registry.lookup()

Documented here: http://code.google.com/p/json-template/wiki/RecommendedApi

And committed here:
http://code.google.com/p/json-template/source/detail?r=e6b1d56a7796816d492dfcd9e
4c845b844510f4b

Original comment by gtempacc...@yahoo.com on 14 Nov 2009 at 8:40

GoogleCodeExporter commented 8 years ago
How can i use @index to indicate odd and even columns in a table?

Original comment by maik.der...@googlemail.com on 5 Jul 2015 at 11:32

GoogleCodeExporter commented 8 years ago
Using @index with table ROWS is more conventional.

COLUMNS probably require <colgroup> and <col> tags.  If the number of columns 
is static (doesn't depend on template data), then you shouldn't need JSON 
Template variables at all.

Most tables I've created have static columns.

Here is an example of @index,

http://chubot.org/json-template/test-cases/testIndex-01.html

You can add a formatter like this:

{@index|even-odd}

even-odd could be registered as a function that returns "blue" or "red" 
depending on the value of @index.

There should be some examples of how to register formatters in the unit tests.

Original comment by andyc...@gmail.com on 5 Jul 2015 at 6:55