Skip to content

Instantly share code, notes, and snippets.

@a-am
Last active June 23, 2022 12:51
Show Gist options
  • Save a-am/8b2a7d9b3f164380acae to your computer and use it in GitHub Desktop.
Save a-am/8b2a7d9b3f164380acae to your computer and use it in GitHub Desktop.
Venti custom calendar output via Twig Helper.
{# Array of months withing a specific month and year #}
{% set events = craft.venti.allEvents().startDate('and', '>= ' ~ prevMonth, '<= ' ~ nextMonth).find() %}
{# Parameters needed for calender output. Venti Events, Month, Year #}
{% set params = { "events": events, "month": currentMonth, "year": currentYear } %}
{% calendar params as days %}
<div>
<table>
<thead>
<tr>
{% for item in calendar.head %}
<th>
<span class="abbr">{{ item.abbr }}</span>
</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% set idx = 1 %}
{% for item in days %}
{% if idx == 1 %}
<tr>
{% endif %}
{% if item.date is not empty %}
<td {% if item.today %}class="today" {% endif %}>
<a class='title'>
{% if item.today %}<span>today</span>{% endif %}
<em>
{% if item.day == 1 %}{{ item.date|date("M") }}{% endif %} {{ item.day }}
</em>
</a>
<ul>
{% for d in item.events %}
<li><a href="{{ d.url }}/{{ d.eid }}">{{ d.title }}</a></li>
{% endfor %}
</ul>
</td>
{% else %}
<td>
<a class='title'><em>{{ item.day }}</em></a>
{% if item.date is not empty %} {{ item.date|date('D M d Y') }} {% endif %}
</td>
{% endif %}
{% set idx = idx + 1 %}
{% if idx > 7 %}
</tr>
{% set idx = 1 %}
{% endif %}
{% endfor %}
</tbody>
</table>
</div>
@amityweb
Copy link

This returns duplicate events on days. It shows events not assigned to that day. Here includes an IF statement to check if the day date matches the event date. Probably not the best way to do it but dont know how else. It still outputs an empty

    due to containing events not for that day. I do not know how to fix the issue of events being added to the day item that are not supposed to be on there:

    {# Getting the current month or year if no month or year are passed as url params #}
    {% set currentMonth = craft.request.getParam('m') ? craft.request.getParam('m') : "now"|date('n') %}
    {% set currentMonthText = craft.request.getParam('m') ? craft.request.getParam('m') : "now"|date('F') %}
    
    {% set currentYear = craft.request.getParam('y') ? craft.request.getParam('y') : "now"|date('Y') %}
    {% set currentYearText = craft.request.getParam('y') ? craft.request.getParam('y') : "now"|date('Y') %}
    
    {# Getting the first day of the currentMonth & currentYear for point of reference to obtain next month and next year #}
    {% set cur = currentYear  ~ "-" ~ currentMonth ~ "-1" %}
    
    {% set prevMonth = cur|date_modify("last day of last month")|date('U') %}
    {% set prevMonthNum = cur|date_modify("last day of last month")|date('n') %}
    {% set prevMonthText = cur|date_modify("last day of last month")|date('F') %}
    
    {% set nextMonth = cur|date_modify("first day of next month")|date('U') %}
    {% set nextMonthNum = cur|date_modify("first day of next month")|date('n') %}
    {% set nextMonthText = cur|date_modify("first day of next month")|date('F') %}
    
    {% set prevMonthYear = cur|date_modify("last day of last month")|date('Y') %}
    {% set nextMonthYear = cur|date_modify("first day of next month")|date('Y') %}
    
    {# Output of the calendar #}
    
    {# Array of months within a specific month and year #}
    {% set events = craft.venti.allEvents().startDate('and', '> ' ~ prevMonth, '< ' ~ nextMonth).find() %}
    
    {# Parameters needed for calender output. Venti Events, Month, Year #}
    {% set params = { "events": events, "month": currentMonth, "year": currentYear } %} 
    
    <h1>Events for {{currentMonthText}} {{currentYearText}}</h1>
    
    <a href="?m={{prevMonthNum}}&y={{prevMonthYear}}">< {{prevMonthText}} {{prevMonthYear}}</a>
    <a href="?m={{nextMonthNum}}&y={{nextMonthYear}}">{{nextMonthText}} {{nextMonthYear}} ></a>
    
    {% calendar params as days %}
    <div>
        <table class="events-calendar">
            <thead>
                <tr>
                    {% for item in calendar.head %}
                        <th>
                            <span class="abbr">{{ item.abbr }}</span>
                        </th>
                    {% endfor %}
                </tr>
            </thead>
            <tbody>
                {% set idx = 1 %} 
                    {% for item in days %} 
                        {% if idx == 1 %}
                            <tr>
                        {% endif %} 
                        {% if item.date is not empty %}
                            <td {% if item.today %}class="today" {% endif %}>
                                <a class='title'>
                                    {% if item.today %}<span>today</span>{% endif %}
                                    <em>
                                        {% if item.day == 1 %}{{ item.date|date("M") }}{% endif %} {{ item.day }}
                                    </em>
                                </a>
                                
                                {% if item.events|length > 0 %}
                                <ul>
                                    {% for d in item.events %}
    	                                {% if item.date == d.dateParts.year~'-'~d.dateParts.month~'-'~d.dateParts.day %}
    	                                     <li><a href="{{ d.url }}/{{ d.eid }}">{{ d.title }}</a></li>
    	                                {% endif %}
                                    {% endfor %}
                                </ul>
                                {% endif %}
                                
                            </td>
                        {% else %}
                            <td>
                                <a class='title'><em>{{ item.day }}</em></a> 
                                {% if item.date is not empty %} {{ item.date|date('D M d Y') }} {% endif %}
                            </td>
                        {% endif %} 
                        {% set idx = idx + 1 %} 
                        {% if idx > 7 %}
                            </tr>
                            {% set idx = 1 %} 
                        {% endif %} 
                    {% endfor %}
            </tbody>
        </table>
    </div>
    

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment