Adding Microformats isn't that difficult

At the end of the last step I had managed to get the Geo microformat into the HTML markup. The next step was to get the hCalendar into the markup.

The trick, from a Drupal themer's point of view, is to have access to that information in a format that can be manipulated. I used the date module to add a datestamp cck field to the event content type. I then added time and date information for each event. So again I move onto formatting. Creating a hCalendar microformat can be extremely detailed, but I was happy with just the basics. According to the documentation I needed just a summary and a start time. I used the title as the summary for now. Note how the Geo microformat is listed as an optional property of the hCalendar microformat, and I have that available already. Let's look at the relevant theme code that now appears in my node-event.tpl.php template.

<?php print $terms; ?><?php } ?> "><?php print $title; ?> <?php print $field_details[0][view]; ?> " class="dtstart"> <?php print $field_time[0][view]; ?>
Lat/Lon: <?php print $field_lat_lon[0][lat]; ?> <?php print $field_lat_lon[0][lon]; ?>

So what's the bare minimum needed for a hCalendar microformat. First of all, the event information is wapper in a div with the class "vevent". Next, I need a summary property. In this case I use the $title variable, and put that inside a link to the full node.
" class="summary url"><?php print $title; ?>

Finally, I need the dtstart property. With the $field_time variable, I have access to the raw value, as $field_time[0][value]. I need to format this as a ISO date, using the php date function, date() . This would be relatively easy in PHP 5, I could have used the 'c' format character. My demo website is still on PHP 4 so I had to use the relatively obscure format you see in use above. As I understand it, I didn't need to use the element, but using it allows me to present one format for human readers, and another format for microformat parsers such as Tails or Operator. This is explained at the microformats wiki as thus

If an 'abbr' element is used for a property, then the 'title' attribute of the 'abbr' element is the value of the property, instead of the contents of the element, which instead provide a human presentable version of the value. This specification recommends that such 'abbr' elements be used for the following iCalendar properties:

  • DTSTART, DTEND, DURATION, RDATE, RRULE

So Its looks right on screen, and it looks right in the code. The outputted HTML for the date looks like this.
Saturday, January 26, 2008 - 17:00 .
The Geo microformat code is within the 'vevent' 'div', so it's a part of the hCalendar microformat. I got an unexpected bonus, mereley by enabling the taxonomy module. The default formatting of the taxonomy module uses, is a rel-tag microformat The default output could be improved, but that's for another day.

Powered by Drupal, an open source content management system