This document is for an older version of

Calendar

.

View latest version →

Templating

calendar.export function

The calendar.export template function lets you export all or specific calendar events as an ICS formatted file. This will cause the user to receive a file download dialogue, and allow them to import the event(s) into a calendaring app like Apple Calendar or Google Calendar.

TIP

Make sure to pass events without loading their occurrences, since the exporter uses the RRule to specify how it recurs!

Parameters

The calendar.export template function is fairly simple in that it receives a list of events generated by the calendar.events function. Additionally, there are 2 optional parameters available:

  • site #
    • Allows you to specify a certain site's translated version to be exported. Use the calendar's handle.
  • timezone #
    • Allows you to simulate a localized timezone. By default, Calendar dates are stored as floating timezones, so an event with start time of 1pm would have a "floating" time of 1pm for UTC±0. If you specify "America/New_York" (UTC-4), the start time of the event will still show as 1pm, but pretend to be on a New York/UTC-4 timezone. If you then viewed this exported event in a timezone outside of UTC-4 such as UTC-5, the start time would display as 12pm. This feature is more-so available as a workaround for Google Calendar not correctly supporting floating timezones.
    • Specify the same format Craft does... Region/City, e.g. timezone: "America/New_York".
    • Warning: Dates/times will be will be localized (changed) for users outside of the specified timezone.
{{ craft.calendar.export(events, {site: "mySiteHandle", timezone: "America/New_York"}) }}
1

Usage in Templates

A basic example with floating timezones (the same dates/times forced for everyone, e.g. 6:30pm is 6:30pm regardless of which timezone you're in):

{%- set events = craft.calendar.events({
  calendarId: [1, 3, 5],
  rangeStart: "1 year ago",
  rangeEnd: "5 years",
  loadOccurrences: false
})) -%}

{%- header "Content-Type: application/octet-stream" -%}
{%- header "Content-Transfer-Encoding: binary" -%}

{{- craft.calendar.export(events) -}}
1
2
3
4
5
6
7
8
9
10
11

An example where you can simulate a localized timezone that will be localized for users outside of the specified timezone (more-so as a workaround for Google Calendar not correctly supporting floating timezones:











 

{%- set events = craft.calendar.events({
  calendarId: [1, 3, 5],
  rangeStart: "1 year ago",
  rangeEnd: "5 years",
  loadOccurrences: false
}) -%}

{%- header "Content-Type: application/octet-stream" -%}
{%- header "Content-Transfer-Encoding: binary" -%}

{{- craft.calendar.export(events, {timezone: "America/New_York"}) -}}
1
2
3
4
5
6
7
8
9
10
11