How can I create an ICS calendar file I want to create an ICS file that people can download to add events to their calendar. I have done this with ExpressionEngine by creating an xml template and just calling it. Below is the code I have so far but I think the problem is that twig doesn't allow php in a template. I'm guessing that means I need to put this in a plugin, but that's beyond my skills. Any help/advice appreciated.

&lt;?php
$ical = "BEGIN:VCALENDAR
X-WR-TIMEZONE:Europe/London
PRODID:-//Orienteering Club//NONSGML v1.0//EN
CALSCALE:GREGORIAN
X-WR-CALNAME:Orienteering Club
VERSION:2.0
{% set events = craft.entries.find({section:'events,activities', eventDate : "&gt;= " ~ now.w3cDate() , order:'eventDate'}) %}
{% if events %}
{% for entry in events %}
BEGIN:VEVENT
UID:{entry_id}
URL;VALUE=URI:{{ entry.url }}
DTSTAMP:{{ entry.eventDate.format('Ymd') }}
DTSTART:{{ entry.eventDate.format('Ymd') }}
DTEND;VALUE=DATE:{{ entry.eventDate.format('Ymd') }}
LOCATION:{{ entry.eventName }}
SUMMARY:Orienteering,nr.{{ entry.map.parts.postal_town }}
END:VEVENT
{% endfor %}
{% endif %}
END:VCALENDAR";

//set correct content-type-header
header('Content-type: text/calendar; charset=utf-8');
header('Content-Disposition: inline; filename=calendar.ics');
echo $ical;
exit;
?&gt;
You don't need a plugin.  Here's some example code from an old project of mine.

{% spaceless %}

    {% set thisPackage = "" %}

    {% set packageId = craft.request.getSegment(2) %}
    {% if packageId %}
        {% set thisPackage = craft.entries.section('packages').id(packageId).find()[0] %}
    {% endif %}
    {% set craftAdminId = craft.users.admin('1').first().id %}

    {# -- Do some sanity checking, make sure they have perms #}

    {% if thisPackage %}
        {% set authorInfo = craft.users.id(thisPackage.authorId)[0] %}
        {% set thisCurrency = thisPackage.currency.value %}
        {% if authorInfo.id != currentUser.id and craftAdminId != currentUser.id %}
            {% redirect siteUrl %}
        {% endif %}
    {% else %}
            {% redirect siteUrl %}
    {% endif %}

    {% header "Content-Type: text/calendar; charset=utf-8" %}
    {% header "Content-Disposition: attachment; filename=cal.ics" %}
{% endspaceless %}
BEGIN:VCALENDAR
VERSION:2.0
METHOD:PUBLISH
X-WR-CALNAME:{{ thisPackage.packageTitle | raw | escape }}
PRODID:-//tastystakes/tourneycal//NONSGML v1.0//EN
{% spaceless %}
{% for block in thisPackage.packageTournaments %}
BEGIN:VEVENT
UID:{{ thisPackage.id }}{{ block.id }}{{ authorInfo.email }}
DTSTART:{{ block.date.getTimestamp() | date_modify("+3 hours") | date("Ymd", "UTC") }}T{{ block.date.getTimestamp() | date_modify("+3 hours") | date("Gi", "UTC") }}00Z
DTEND:{{ block.date.getTimestamp() | date_modify("+3 hours") | date_modify("+" ~ block.days ~ " days") | date("Ymd", "UTC") }}T{{ block.date.getTimestamp() | date_modify("+3 hours") | date_modify("+" ~ block.days ~ " days") |date("Gi", "UTC") }}00Z
SUMMARY:{{ block.tourneyName }}
DESCRIPTION:{{ block.tourneyName }}
END:VEVENT
{% endfor %}
END:VCALENDAR
{% endspaceless %}