I am fairly new to twig and templating with Craft (and I have inherited this code, so that makes it even more fun).
I have a calendar page that lets you filter the events in the calendar based on a venti group (using the Venti Calendar plugin to create those groups).
How it currently works is that whatever group filter is selected, the url updates to have that group ID as the last segment.
Filter Names and their IDs: Cinema = 1, Theatre = 2, Events = 3, Education = 4
If a user is looking at the events for the month of December 2017 with no filters, the url looks like this:
example.com/month/2017/12
If a user is looking at the events for the same date, but has a filter applied (in this case, the "Cinema" filter), the url looks like this:
example.com/month/2017/12/1
Cinema has group ID of 1
so 1 is the last segment.
In order to change the applied filter without resetting the date, I want to trim the last segment from the url, and replace with the new ID as the last segment. Doing this works completely as expected unless the last segment and the last character of the previous segment are the same number (e.g., example.com/month/2017/12/2
).
In this case, if I am currently on this URL, and I want to change to a different filter (in this example we change to use Cinema Filter = 1), the following happens.
example.com/month/2017/1/1
where it should actually be example.com/month/2017/12/1
So what is happening is that it is trimming the 2
from the last segment and the segment before the last segment.
Here's the code for the filter buttons:
{% set groups = craft.venti.groups() %}
{% for group in groups %}
<a class="button" href="
{% spaceless %}
{% if craft.request.getLastSegment() in ['1','2','3','4'] %}
{{url(craft.request.path)|trim('/' ~
craft.request.getLastSegment())}}/{{group.id}}
{% else %}
{{url(craft.request.path)}}/{{group.id}}
{% endif %}
{% endspaceless %}">
{{ group }}
</a>
{% endfor %}
If I remove '/' ~
from this:
{{url(craft.request.path)|trim('/' ~ craft.request.getLastSegment())}}/{{group.id}}
It actually works perfectly, save for the fact that the new url looks like this: example.com/month/2017/12//1
With the addition of that second backslash.
Can anyone see what I am doing wrong here, or provide any help? If you need more of my code, please let me know.
Thank you!