| Issue 28: | Provide @index variable for the current array index | |
| 2 people starred this issue and may be notified of changes. | Back to list |
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.
Sep 26, 2009
Project Member
#1
gtempacc...@yahoo.com
Status:
Started
Nov 7, 2009
Done in JS and PHP too
Nov 7, 2009
(No comment was entered for this change.)
Labels:
Todo-Java
Nov 8, 2009
(No comment was entered for this change.)
Summary:
Provide @index variable for the current array index
Nov 9, 2009
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.
Nov 9, 2009
@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.
Nov 9, 2009
Yeah, that doesn't work. Error: exception thrown and not caught, debuger drops me at
throw {
name: 'UndefinedVariable', message: name + ' is not defined'
}
Nov 9, 2009
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.
Nov 9, 2009
Update worked right out of the box, thanks!!
Nov 14, 2009
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: https://code.google.com/p/json-template/wiki/RecommendedApi And committed here: https://code.google.com/p/json-template/source/detail?r=e6b1d56a7796816d492dfcd9e4c845b844510f4b
Jul 5, 2015
How can i use @index to indicate odd and even columns in a table?
Jul 5, 2015
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. |