David Emery Online

Hi there, I’m David. This is my website. I work in music for Apple. You can find out a bit more about me here. On occasion I’ve been known to write a thing or two. Please drop me a line and say hello. Views mine not my employers.

Signup to receive the latest articles from de-online in your inbox:

Textpattern and iCal

5 May 2006

Today, you get a little how to for creating iCal compatible calendars from Textpattern. This is most useful if you use Textpattern to store events, and really useful if you use it to store gigs in, like The Rogers Sisters Website does. The following requires a bit of textpattern knowledge (of various txp: tags and the like), but I’m going to try and make it as clear as possible.

Just for a bit of background, the gigs on the Rogers Sisters site are stored as articles in a section called “gigs”, with the venue and place as the article title, any further description (like who’s supporting) in the body, and the gig date as the posted date.

Then to display the gigs you simply use an article_custom tag, with the date set to future like this:

<txp:article_custom form="gig" time="future" section="gigs" sortdir="asc" limit="25" />

Now, on to the calendar! To start with, the file format that iCal uses is called .ics, and is used by several other calendar programs so hopefully this should work with any .ics compatible program. The key piece of information that makes this all fairly straightforward is that .ics is a text based format, and a fairly simple one at that, so we can output a page from textpattern that complies to the .ics format, and can hence be used in iCal.

First, set up a new section in Textpattern (in presentation -> sections) – I called mine “gigcalendar”, and make it use a new blank page by creating a new page in the pages tab, and then setting the section to use it.

Next, copy the code below into the blank page (making sure you don’t have anything else, including any html, on the page):

BEGIN:VCALENDAR
VERSION:2.0
X-WR-CALNAME:Gigs Calendar
PRODID:-//Apple Computer, Inc//iCal 1.5//EN
X-WR-TIMEZONE:Europe/London
<txp:article_custom form="gigsicsitem" time="any" section="gigs" sortdir="asc" limit="10000"/>
END:VCALENDAR

Now, what this does it output the required .ics header and footer and then get the gigs we want to show up in the calendar (we’re after both past and future gigs, hence the
time=”any”), using the “gigsicsitem” article form, which is what outputs each event.

Next, go over to the forms tab and create a new article form (what I’ve called ‘gigsicsitem’), and past in the code below:

BEGIN:VEVENT
DTSTART:<txp:posted format="%Y%m%d" />T193000
DTEND:<txp:posted format="%Y%m%d" />T230000
SUMMARY:<txp:title />
END:VEVENT

This simply outputs each event in the correct formatting for .ics use. The DTSTART and DTEND are probably the only things that need explaining. These are the start and end times of the event, the first part of which is the date in yyyymmdd format, which we create using the “format” argument of the txp:posted tag. The second part is the exact time of the event (hence it starting with “T”). As we’re dealing with gigs, I’ve made some assumptions as most gigs start at 19:30ish and end at 23:00, so I’ve hard coded the times in. You could, however, quite easily use different formatting on the txp:posted tag to get the posted time as well. To make the end date you could always use a custom field as well, as textpattern only has one date field.

After you’ve done all that, your “gigscalendar” section should be outputting a valid .ics file, so simply take the url ( e.g. http://www.yoursite.com/gigscalendar ) and in iCal under the “Calendar” menu, click the “Subscribe…” menu item and enter the address.

That’s it! Hope this might be useful to someone out there…