Skip to content

Instantly share code, notes, and snippets.

@iamkirkbater
Created July 11, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iamkirkbater/61a1f04b069b8b10296ca11b4032f429 to your computer and use it in GitHub Desktop.
Save iamkirkbater/61a1f04b069b8b10296ca11b4032f429 to your computer and use it in GitHub Desktop.
Twig Calendar in divs format because tables are hard for responsive. Don't remember where I got this from originally, so if you know or see it let me know and I'll attribute.
{#
Creates a faux table twig calendar.
Takes the following variables:
month
currentDay
calendarContent
month 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 month = "now"|date("U") %}
{% set month = "December 2012"|date("U") %}
currentDay is an integer of the current day, 1-31.
How ever you want to output items onto the calendar is a different issue,
but I'd assume pushing everything into an array numerically indexed by that day:
$calendarContent = array(1=>array('Something on the first'),31=>array('Halloween'));
#}
<div class="calendar">
<div class="calendar__header">
<div class="calendar__title">
{{ month|date('F Y') }}
</div>
<div class="calendar__row">
<div class="calendar__day calendar__heading">S<span class="calendar__heading--abbr">un</span><span class="calendar__heading--full">day</span></div>
<div class="calendar__day calendar__heading">M<span class="calendar__heading--abbr">on</span><span class="calendar__heading--full">day</span></div>
<div class="calendar__day calendar__heading">T<span class="calendar__heading--abbr">ue</span><span class="calendar__heading--full">sday</span></div>
<div class="calendar__day calendar__heading">W<span class="calendar__heading--abbr">ed</span><span class="calendar__heading--full">nesday</span></div>
<div class="calendar__day calendar__heading">T<span class="calendar__heading--abbr">hu</span><span class="calendar__heading--full">rsday</span></div>
<div class="calendar__day calendar__heading">F<span class="calendar__heading--abbr">ri</span><span class="calendar__heading--full">day</span></div>
<div class="calendar__day calendar__heading">S<span class="calendar__heading--abbr">at</span><span class="calendar__heading--full">urday</span></div>
</div>
</div>
<div class="calendar__body">
<div class="calendar__row">
{% set daysInMonth = month|date('t') %}
{% set startDow = month|date('F 1\\s\\t Y')|date('w') %}
{% set dow = startDow %}
{% for day in range(1,daysInMonth) %}
{% if loop.first and startDow != 0 %}
<div class="calendar__span calendar__begin calendar__span--spans-{{ startDow }}"></div>
{% endif %}
<div class="calendar__day {% if (currentDay | date('j')) == day %}calendar__day--current{% endif %}">
<div class="calendar__number">{{ day }}</div>
<div class="calendar__day-content">
{{ calendarContent[day] }}
</div>
</div>
{% if loop.last and dow != 6 %}
<div class="calendar__span calendar__span--spans-{{ 6 - dow }}">&nbsp;</div>
{% endif %}
{% if dow == 6 %}
{% set dow = 0 %}
</div>
<div class="calendar__row">
{% else %}
{% set dow = dow + 1 %}
{% endif %}
{% endfor %}
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment