Skip to content

Instantly share code, notes, and snippets.

@elivz
Created June 20, 2015 04:17
Show Gist options
  • Save elivz/9a0c48d15d7307fcd89f to your computer and use it in GitHub Desktop.
Save elivz/9a0c48d15d7307fcd89f to your computer and use it in GitHub Desktop.
Event calendar using Craft & Twig
{% set allEvents = craft.entries('events') %}
{% for month, events in allEvents|group('startDate|date("F Y")') %}
{% set eventsByDate = events|group('startDate|date("j")') %}
<section id="{{ month|slugify }}" class="month{% if loop.first %} currentMonth{% endif %}">
<h1 class="monthName">{{ month }}</h1>
<div class="eventList">
<table class="calendar">
<thead>
<tr>
<th width="14%">Sunday</th>
<th width="14%">Monday</th>
<th width="14%">Tuesday</th>
<th width="14%">Wednesday</th>
<th width="14%">Thursday</th>
<th width="14%">Friday</th>
<th width="14%">Saturday</th>
</tr>
</thead>
<tbody>
<tr>
{% set daysInMonth = month|date('t') %}
{% set startRow = month|date('F 1\\s\\t Y')|date('w') %}
{% set row = startRow %}
{% for day in range(1, daysInMonth) %}
{% if loop.first and startRow != 0 %}
<td colspan="{{ startRow }}">&nbsp;</td>
{% endif %}
<td>
<div>
<div class="calendar-date">{{ day }}</div>
<div class="calendar-events">
{% if eventsByDate[day] is defined %}
{% for event in eventsByDate[day] %}
<a href="{{ event.url }}">
<time datetime="{{ event.startDate.w3cDate }}" class="time">{{ event.startDate.format('g:iA') }}</time>
<strong>{{ event.title }}</strong>
</a>
{% endfor %}
{% endif %}
</div>
</div>
</td>
{% if loop.last and row != 6 %}
<td colspan="{{ 6 - row }}">&nbsp;</td>
{% endif %}
{% if row == 6 %}
{% set row = 0 %}
</tr>
<tr>
{% else %}
{% set row = row + 1 %}
{% endif %}
{% endfor %}
</tr>
</tbody>
</table>
</div>
</section>
{% else %}
<p class="no-results">Nothing is currently scheduled.</p>
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment