I have a section for holding calendar entries stored by postDate in the CMS. On the front end I want to basically display these in a calendar.
I have code which displays the calendar looping through each day of the month. For each day it currently queries if there is any entries for this day.
Is there a better way to do it? - my current method had a query for each day?
Current code is:
<table class="cal">
<thead class="hide-for-small">
<tr>
<th width="300">Sun</th>
<th width="300">Mon</th>
<th width="300">Tue</th>
<th width="300">Wed</th>
<th width="300">Thu</th>
<th width="300">Fri</th>
<th width="300">Sat</th>
</tr>
</thead>
<tbody>
<tr>
{% set time = "now"|date("U") %}
{% set daysInMonth = time|date('t') %}
{% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
{% set dow = startDow %}
{% for day in range(1,daysInMonth) %}
{% if loop.first and startDow != 0 %}
<td colspan="{{ startDow }}"></td>
{% endif %}
<td>
<div>
<span class="day">{{ day }}</span>
{% set today=thisyear ~ "/" ~ thismonth ~ "/" ~ day %}
{% if (category is defined) and (category is not empty) %}
{% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d'), relatedTo: category } %}
{% else %}
{% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d') } %}
{% endif %}
{% set entries = craft.entries(params)%}
{% if entries|length %}
<div>
{% for entry in entries %}
<span class="event green">{{ entry.title }}</span>
{% endfor %}
</div>
{% endif %}
</div>
</td>
{% if loop.last and dow != 6 %}
<td colspan="{{ 6 - dow }}"> </td>
{% endif %}
{% if dow == 6 %}
{% set dow = 0 %}
</tr>
<tr>
{% else %}
{% set dow = dow + 1 %}
{% endif %}
{% endfor %}
</tr>
</tbody>
</table>