Skip to main content

Event Query

The calendar.event template function fetches a single event by its unique ID or slug.

Parameters

First parameter is the ID or slug of the event, the second parameter is an object of optional parameters listed below

  • occurrenceDate #
    • Specify an occurrence date if you wish to show a recurring event for a specific date
  • occurrenceRangeStart #
    • A date string to specify the time range for which to start fetching occurrences for a recurring event
    • You can use a Y-m-d string
    • A relative string like 6 months ago
  • occurrenceRangeEnd #
    • A date string to specify the end date
  • occurrenceLimit #
    • Specify an int for hard limiting the amount of occurrences fetched. This defaults to 200, since never-ending recurrences must have a limit
  • status #
    • Specify null to include disabled events. Defaults to showing enabled events only.

Usage in Templates

{% set event = craft.calendar.event(15, {
occurrenceLimit: 15,
occurrenceRangeStart: "today",
occurrenceRangeEnd: "6 months"
}) %}

{% if event %}
{{ event.title }}<br>
{{ event.startDate.format("l d, Y") }}
{% endif %}

You can also access any custom fields which you have added to events by directly calling their handle (the handle must not match any of the existing event properties). If you had a field called Event Description with a handle of eventDescription you would access it in the template like this:

{{ event.eventDescription }}

You could make sure such a property exists first, to prevent errors in case the field is deleted:

{% if event.eventDescription is defined %}
{{ event.eventDescription }}
{% endif %}

Let's use a Date method to check if our event's start date is before the current Day's date

{# Print out the Y-m-d of start date if start date is before the Day date #}

{% if event.startDate.lt(day.date) %}
{{ event.startDate.dateString }}
{% endif %}

Now let's print out the duration of the event, which is the amount of time in months, days, hours, etc from the event's start date until its end date

{# Print out the duration string #}
{# An example output would be "1d 5h 30m" #}

{{ event.duration.humanReadable }}

{# Print out a custom message if the duration in days is 1 or more #}

{% if event.duration.days >= 1 %}
Event spans for {{ event.duration.days }} days
{% endif %}