Mike Carlton <[EMAIL PROTECTED]> wrote:
> Does anyone have suggestions on how to cleanly code up a monthly
> calendar application with H::T?
>
> I've currently got a perl script that generates an html table and the
> cell contents -- this works fine, but is a royal pain to update the
> html.  So it occurs to me that I would like to set this up as a
> template.
>
> The table cell for each day of the month will have a list of links --
> this is straightforward enough.  But I can't see how to code all the
> possible calendar formats in H::T -- there are months with 4, 5 or 6
> weeks.  The number of days in a month varies, the number blank days
> before the 1st and after the last day varies, etc.

i wrote a calendar application using HTML::Template and
CGI::Application that you can see at http://asiasociety.org/events/

it sounds like you need something a bit different.  this is a fixed-size
html "calendar widget" with each day containing only the date itself,
linked to results starting on that date, while you need a larger
stretchy calendar with more lengthy cell contents.

also this widget doesn't show just the 12 "calendar months".  it can
start in any week and always shows five weeks, so there are cases where
the dates shown span *three* calendar months, even though at first
glance it appears to be just like a wall calendar with one month per
page... in any case the logic to generate the calendar itself with
HTML::Template goes something like this:

the first day of the first week of my calendar is the most recent sunday
previous to today (or today if today is sunday) so i started with:
  $now = time() (unix date/time in seconds) and:
  @date = localtime($now), to get today's date.

(and i fixup the zero-based month elements, adding 1, and 1900-based
year adding 1900, of course.)

one of my @date elements is:
  $day_of_week, (0-6) == (Sun-Sat).

so to get back to sunday i need:
  $seconds_in_a_day = 60*60*24
  $seconds_since_sunday = $seconds_in_a_day * $day_of_week

which gives us:
  $sundays_time = $now - $seconds_since_sunday, and
  @sundays_date = localtime($sundays_time);

my template uses a tmpl_loop named "weeks" for the weeks (table rows)
and the week tmpl_loop contains a nested tmpl_loop named days something
like this:

<table>
    <!-- header row -->
    <tr>
      <th>Sun</th>
      <th>Mon</th>
      <th>Tue</th>
      <th>Wed</th>
      <th>Thu</th>
      <th>Fri</th>
      <th>Sat</th>
    </tr>
  <tmpl_loop name="weeks">
    <tr>
      <tmpl_loop name="days">
        <td>
          today is:
          <tmpl_var month>/<tmpl_var date>/<tmpl_var year>!
        </td>
      </tmpl_loop>
    </tr>
  </tmpl_loop>
<table>

to populate this template, i needed  a fairly complicated data
structure.  i needed "weeks" to be passed to the template as an arrayref
representing the rows in the table, and within each iteration of the
weeks loop, i need a *nested* loop named "days" representing the days in
that week (the cells of the table).  each day in days needed to be a
hashref containing whatever actual named tmpl_vars i needed to generate
the html for that day's table cell.

to create this data structure i started with $this_days_time =
$sundays_time, created a perl loop to iterate over the 5 weeks, and an
inner loop to iterate over each 7 days, incrementing $this_days_time +=
$seconds_in_a_day, and passing that value to localtime to get back a
@date array, and populate the day's cell, to end up with something like
this:

$weeks= [
  {
    days=> [
      {month=>9, date=>8, year=>2002},
      {month=>9, date=>9, year=>2002},
      {month=>9, date=>10, year=>2002},
      {month=>9, date=>11, year=>2002},
      {month=>9, date=>12, year=>2002},
      {month=>9, date=>13, year=>2002},
      {month=>9, date=>14, year=>2002},
    ]
  },
  {
    days=> [
      { etc. },
      { etc. },
    ]
  },
  ...
];

with all five weeks containing seven days each.

like i said my calendar widget has a fixed number of five weeks in a
sort of "moving window" that can start in the middle of a month, span
more than one month, etc.  but the
same type of logic could be used to compute
$days_since_the_sunday_before_the_first_of_the_month :-) and start from
there, "blanking" any cells whose month != the current month.

hope this helps,

-dave

PS: cut-n-pasters, beware: all the above template and perl code is from
memory and may cause cheese to exude from your cd-rom when invoked.



-------------------------------------------------------
This sf.net email is sponsored by: OSDN - Tired of that same old
cell phone?  Get a new here for FREE!
https://www.inphonic.com/r.asp?r=sourceforge1&refcode1=vs3390
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to