Week object
A Week object holds in itself a list of all Events that fall into this week, as well as gives the possibility to iterate over Day object's that fall into the range of this Week object.
Properties
date- A Date object of the starting date of the event duration
- For a Week object of April 15, 2023 and week starting day of Sunday, this would return the Date object of
2023-04-10with the starting day of the week set to Sunday
startDate- Basically the same as the
dateproperty
- Basically the same as the
endDate- The ending date of the Week object's week, e.g -
2023-04-16in this case
- The ending date of the Week object's week, e.g -
previousDate- Returns a Date object of the previous week relative to the
dateproperty, e.g.2023-04-03in this case
- Returns a Date object of the previous week relative to the
nextDate- Returns a Date object of the next week relative to the
dateproperty, e.g.2023-05-17in this case
- Returns a Date object of the next week relative to the
events- Contains a list of Events that fall into this Week object's range
eventCount- The number of events in the
eventsproperty
- The number of events in the
Methods
dateRange(int before, int after)- Returns a list of Date objects ranging
beforenumber of weeks beforedateandafternumber of weeks afterdate - E.g.
week.dateRange(6, 6)would return a list starting from2023-02-28and ending in2023-05-22
- Returns a list of Date objects ranging
containsDate(dateObject)- Returns true if the
dateObjectprovided is within the Week object's date range - E.g.
week.containsDate(event.startDate)would return true if the Week object would be April 15, 2023 starting on Sunday, and theevent.startDatewould be2023-04-10, but false if theevent.startDatewould be2023-04-09
- Returns true if the
Usage in Templates
Iterate through days of the week
{% set week = craft.calendar.week({
date: "today",
calendar: ["holidays", "sports"]
}) %}
{% for day in week %}
{% for event in day.events %}
<div style="{{ event.calendar.color }};">
{{ event.title }}
</div>
{% endfor %}
{% endfor %}
Iterate through all events in the week
{% set week = craft.calendar.week({
date: "today",
calendar: ["holidays", "sports"]
}) %}
{% for event in week.events %}
<div style="{{ event.calendar.color }};">
{{ event.title }}
</div>
{% endfor %}
Show only events which start in weekdays
{% set week = craft.calendar.week({
date: "today",
calendar: ["holidays", "sports"]
}) %}
{% for event in week.events if event.startDate.weekday %}
<div style="{{ event.calendar.color }};">
{{ event.title }}
</div>
{% endfor %}