Skip to content

Instantly share code, notes, and snippets.

@jasongabler
Forked from aknosis/calendar.twig
Last active January 17, 2018 21:26
Show Gist options
  • Save jasongabler/8da723263a88244016ed75e6cf63eff0 to your computer and use it in GitHub Desktop.
Save jasongabler/8da723263a88244016ed75e6cf63eff0 to your computer and use it in GitHub Desktop.
Table based calendar only using Twig
{#
time can be any string acceptable by http://www.php.net/strtotime, the
template will output that time's month.
If you don't want to pass in a date you can set time like this:
{% set time = "now"|date("U") %}
{% set time = "December 2012"|date("U") %}
Data (in activityByDay) is collated by activityByDay[year][month][day], which holds a list
of activity data for each day. YMMV for your own needs.
#}
{% for year,months in activityByDay %}
{% for month, days in months %}
{% set execution = days|first|first %}
{% set time = execution.actionStamp|date("U") %}
<table class="table table-bordered">
<thead>
<tr class="month-name">
<th colspan="7" class="text-center">{{ time|date('F Y') }}</th>
</tr>
<tr class="week-name">
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
</thead>
<tbody>
<tr>
{% 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>
<div>{{ day }}</div>
<div class="day">
{% if days[day] is defined %}
{% for activity in days[day] %}
<a href="{{ path('activity_page', {'activityId':activity.id}) }}">{{ activity.name }}</a><br>
{% endfor %}
{% endif %}
</div>
</div>
</td>
{% if loop.last and dow != 6 %}
<td colspan="{{ 6 - dow }}">&nbsp;</td>
{% endif %}
{% if dow == 6 %}
{% set dow = 0 %}
</tr>
<tr>
{% else %}
{% set dow = dow + 1 %}
{% endif %}
{% endfor %}
</tr>
</tbody>
</table>
{% endfor %}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment