For future reference:
This is how to make every third row off-color in a table listing when using Template Toolkit.
[% FOREACH object = domain.raObject %]
Re: Template Toolkit: Alternating row colors
davorg on 2004-12-14T15:32:14
Using loop.count instead of loop.index makes that code a little simpler (loop.count is always loop.index + 1).
[% FOREACH object = domain.raObject %] <tr [% IF not (loop.count % 3) %]bgcolor="#EEEEEE"[% END %]>
Also, I'd tend to do something like this with classes and stylesheets as it makes it easier to change in the future.
[% FOREACH object = domain.raObject %] <tr class="row_style_[% loop.count % 3 %]">
You can then define three (or less) styles called "row_style_0", "row_style_1" and "row_style_2".
Re: Template Toolkit: Alternating row colors
Dom2 on 2004-12-14T16:09:48
I prefer to do that sort of fiddling in JavaScript, all though it's all much of a muchness. There's a good article on how to do stripey tables. Again, I prefer class names to the technique described, but it's a small change.-Dom
Re: Template Toolkit: Alternating row colors
merlyn on 2004-12-14T19:27:09
Javascript? If Javascript is the answer, you asked the wrong question!Re: Template Toolkit: Alternating row colors
Dom2 on 2004-12-14T21:45:48
I beg to differ. JavaScript shares a lot in common with Perl, not least the fact that it has a very bad reputation due to people writing very bad things with it.:-) JavaScript is a pleasant language, fairly small with a regular syntax and quite object oriented. It's got closures and objects and garbage collection and exceptions and all the things that you expect from a modern dynamic language. It's worth a second look if all you've ever done is bitch at badly written sites that use it...
-Dom
Re: Template Toolkit: Alternating row colors
malte on 2004-12-14T22:35:07
I second that! JavaScript is a really nice language (especially if you keep IE 5 for Macintosh out of the game). What Do you guys think about this?
The other good thing about JavaScript is: If you know it your an instant expert on ActionScript (Macromedia Flash programming language)
:). At work, we seem to hav found at least a couple uses for Flash that are not annoying :) Re: Template Toolkit: Alternating row colors
merlyn on 2004-12-15T03:09:30
That may all be fine, but you forget that more people have Javascript turned off today than ever before (including turned off by corporate mandate), and that javascript has variant behaviors on different browsers, and that some people don't even have the option of turning javascript on because their corporate or school gateway filters it out.You must design your website so that it can function without Javascript (although possibly degraded).
Re: Template Toolkit: Alternating row colors
petdance on 2004-12-15T05:35:53
You must design your website so that it can function without Javascript"Must" is such a heavy word. I usually reply "or what?"
Design tradeoffs are tradeoffs.
He already said or what
btilly on 2004-12-16T01:27:12
Or you unavoidably lose some fraction of your potential user base. I agree with him but I'd be curious to see what that fraction is.Re: Template Toolkit: Alternating row colors
Dom2 on 2004-12-15T08:03:00
I agree entirely with you. JavaScript should (where possible) be an addon to the design, which must be fully functional without it.But the point I was trying to make was that the language itself is worthy of closer inspection.
-Dom
Re: Template Toolkit: Alternating row colors
lachoy on 2004-12-15T15:25:57
I agree, it's a nice little language. I used the pure-Java Rhino interpreter for an internal web testing product I built in Java. There are some weird API contortions to perform certain actions (like putting classes/objects in context and translating JS to Java obects), but overall it's quite useful. The E4X stuff recently added (to 1.6R1) looks pretty interesting as well.Re: Template Toolkit: Alternating row colors
zatoichi on 2004-12-15T01:54:47
Why would you want to include yet another js script when you can do the same thing in TT in a few lines of Perl? That is just crazy.
Then again I am retentive about controlling my code as much as possible.
:-) Re: Template Toolkit: Alternating row colors
jplindstrom on 2004-12-14T20:27:25
Thanks!
Being new to Template Toolkit I had to google just to find that little piece of code.Tie::Cycle
petdance on 2004-12-14T16:13:10
brian d foy's Tie::Cycle is ideal for this, although I'm not sure how it'll fit into TT. Maybe I'll write a plugin wrapper for it.Re:Tie::Cycle
petdance on 2004-12-14T16:51:28
Oh look, it seems to already exist:
Template::Plugin::CycleRe:Tie::Cycle
davorg on 2004-12-14T17:32:48
That isn't actually based on Tie::Cycle, it's a complete new reimplementation of the functionality.
Also it's not a properly written TT plugin. It doesn't inherit from Template::Plugin so you can't use it from a template like this:
[% USE Cycle('foo', 'bar') -%]
[% Cycle %]
[% Cycle %]
[% Cycle %]If you replace Cycle.pm with the version that you'll find at http://dave.org.uk/template/ then it will work as expected.
$ tpage test.tt
foo
bar
fooI'll submit the patch to the author tonight.
Re:Tie::Cycle
petdance on 2004-12-14T17:42:33
I don't mind if it's based on Tie::Cycle or not. I just want the functionality.Thanks for the patch. I hope Adam can crank it out soon. I wasn't too keen on having to pass in an iterator in the process() call.