BlogMatrix
 

Something I'd like to see in the Cheetah Template language

edit David P. Janes 2006-09-28 11:14 UTC add comment  ·  ·

All the pages you see here are produced by the Cheetah template language. We're pretty happy with it, though it looks to some degree that the world is standardizing around Django's competing package.

There is one lovely feature of Django that I'd like to see in Cheetah -- the ability to know where you are in a for loop:

forloop.counter The current iteration of the loop (1-indexed)
forloop.counter0 The current iteration of the loop (0-indexed)
forloop.revcounter The number of iterations from the end of the loop (1-indexed)
forloop.revcounter0 The number of iterations from the end of the loop (0-indexed)
forloop.first True if this is the first time through the loop
forloop.last True if this is the last time through the loop
forloop.parentloop For nested loops, this is the loop "above" the current one

Cheetah requires the decidely unlovely formulation of this ...

#set $sep = '' 
#for $name in $names
$sep$name
#set $sep = ', '
#end for

... to make a comma seperated list of names (I know about the other way for trivial lists, this is an example for illustration). I'd much rather do:

#for $name in $names 
$name
#if $forloop.is_last then "" #else ","#
#end for

Or even (thinking more):

#for $name in $names 
$name
$forloop.if_not_last(",")
#end for 

At some point we may try template translation tricks, not only (or possibly) to move Cheetah to Django templates (if they are seperable from the platform) but also to covert MovableType and Blogger templates to our internal format.

Add Comment