Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jplindstrom (594)

  (email not shown publicly)

Journal of jplindstrom (594)

Tuesday December 14, 2004
09:49 AM

Template Toolkit: Alternating row colors

[ #22306 ]

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 %]
  <tr [% IF not ((loop.index + 1) % 3) %]bgcolor="#EEEEEE"[% END %]>

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • 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

    • 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.


      • Javascript? If Javascript is the answer, you asked the wrong question!
        • Randal L. Schwartz
        • Stonehenge
        • 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...


          • 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 :)

          • 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).

            • Randal L. Schwartz
            • Stonehenge
      • 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. :-)

    • Thanks!

      Being new to Template Toolkit I had to google just to find that little piece of code.
  • 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.


    • Oh look, it seems to already exist: Template::Plugin::Cycle []


      • 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 with the version that you'll find at then it will work as expected.

        $ tpage

        I'll submit the patch to the author tonight.

        • 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.