Continuing the project I started with quite some time ago and just completing recently (for the regular definition of "completed" - i.e., release early release often), I've decided to make my front page a bit more dynamic. So I started by doing some simple stuff. For example, our group's meeting dates are already in a CSV table, so I just created a function in my plugin that extracted it from a table with "YEAR,MONTH,DAY,..." as the first column headers, converted it to something readable ("Monday, May 8, 2006"), et voila, I got what I wanted. Plop it into a cron entry, which both runs ttree and a small ftp upload script I also have, and it's about as dynamic as it needs to be.

The next step was to put descriptions of upcoming events. However, I'm figuring that I don't need to continue advertising upcoming events after they passed. So a bit more magic, and I came up with this:

[%- USE date %] [%- SET today = date.format(date.now, "%Y%m%d") %]
and then ...
[% IF today < 20060406 %] <p>Guess what. On April 6, 2006, we're going to do something special! On April 7th, this paragraph disappears! Neat!</p> [% END %]
The only minor annoyance is that my editor of choice seems to think that the < sign there is the open character for an HTML tag. But that's really quite minor.

What I'm wondering is if anyone can see, or better yet, already uses, a more elegant solution. Perhaps a filter? Is there such a beast built in that I'm missing? Or perhaps that I haven't figured out the right keywords for searching on CPAN? I imagine this would be easy for TT2 experts, but I'm just not seeing it today for some reason.

By "elegant" I mean a few things. First, the template must be readable. Putting in unix timestamps (seconds since epoch) isn't readable ;-). Second, it should be able to handle both begin and end dates. So I may not want to start displaying some text until a certain date, and/or I may want to stop displaying some text after a certain date. The simple numeric comparison above meets that through simple numeric comparisons. However, a bit more obvious to me would be something like:

[% FILTER ByDate start = '2006-04-15' until = '2006-06-28' %] This is only visible from April 15 through June 28, 2006. [% END %]
Obviously, since this is a static document, I'll have to ensure the cron job gets run daily at an appropriate time. But that's somewhat outside the scope of my question.

Is this reasonable/normal usage of TT2? Is there something I'm overlooking?


In reply to Date-sensitive output with Template Toolkit by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.