Fog Creek Software
Discussion Board

Knowledge Base
Terry's Tips
Darren's Tips

Accessing loop counter ?

Any way to in CityScript:

1. write the loop counter (e.g. "current article number is" {$x$})

2. reference the total loop count (e.g. "total number of articles is:"...)


Patrick Thomas

Patrick Thomas
Tuesday, April 9, 2002

Something like this would be handy to allow separators.  If there were a way to test if this is the first or last record then you can conditionally include HTML to separate items.  My definition of a separator is something that doesn't appear before the first record or after the last record.

Bill Graziano
Tuesday, April 9, 2002

How about hard coding the first reference and then using a loop for all the others?

<hard coded link> {$foreach$} , <one>, <two>, etc.{$next$}

Mark W
Tuesday, April 9, 2002

You can create a counter very easily using JavaScript. Here's how to present a count of the articles listed in an index:

1. Place a script block above the ForEach loop setting a counter variable to 0. The keyword "var" is optional but I add it when the variable is first initialized as a marker to myself.

  var loopCounter = 0;

2. At the beginning of the loop insert a script block to add 1 to the counter variable.

  loopCounter += 1;

3. After the loop insert a script block to display the count.

  document.writeln("The number of articles is: " + loopCounter");

Better still, place the first two script blocks into CityDesk variables.

I've updated the example on my website to include the loop counter. You can get it at:

My website is up but I am still working on the content. Feel free to check it out at:

The example cty file will be linked to the Tips page this weekend but you can download it using the direct link above.

CityDesk and JavaScript make a very powerful combination.

Wednesday, April 10, 2002

*  Recent Topics

*  Fog Creek Home