Skip to content

Instantly share code, notes, and snippets.

@umkasanki
Created August 30, 2023 12:03
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 umkasanki/8709d8ec1cb37c2ddf984a59718e275b to your computer and use it in GitHub Desktop.
Save umkasanki/8709d8ec1cb37c2ddf984a59718e275b to your computer and use it in GitHub Desktop.
CraftSMS + Sprig. Entries filter sample
parent template:
<h1>Filter</h1>
<hr>
{{ sprig('_inc/filter', {'limit': 3}) }}
{{ sprig.script }}
Child template:
{% set page = page ?? 1 %}
{% set catIds = catIds ?? [] %}
{% set catSlugs = craft.app.request.param('categories') ?? {} %}
{% set entryQuery = craft.entries.section('blog').relatedTo(catIds).limit(5) %}
{% set pageInfo = sprig.paginate(entryQuery, page) %}
{% set entries = pageInfo.pageResults %}
{% set categoriesQr = craft.categories.group('blogCategories') %}
{% set categoriesIds = categoriesQr.ids() %}
{{ d(catIds) }}
{% for cat in categoriesQr.all() %}
{% set urlParams = urlParams ?? '' %}
{% set urlParams = urlParams ~ ('?page=' ~ page ) %}
{% if cat.id in catIds %}
{% set catSlugs = catSlugs|merge([cat.slug]) %}
{% endif %}
<label for='{{ cat.id }}'>
<input id='{{ cat.id }}'
sprig s-val='catIds'
{% if cat.id in catIds %}
checked
{% endif %}
type='checkbox'
name='catIds[]'
value='{{ cat.id }}'>
{{ cat.title }}
</label>
{% endfor %}
<ul>
{% for entry in entries %}
<li>
<strong style='font-weight: bold'>{{ entry.title }}</strong>
{% for blogCategories in entry.blogCategories.all() %}
{{ loop.first ? 'Tags: ' }}
{{ blogCategories.title }}
{{- not loop.last ? ',' }}
{% endfor %}
</li>
{% endfor %}
</ul>
{% if entries %}
<p>
<em>Page {{ pageInfo.currentPage }} of {{ pageInfo.totalPages }} pages.</em><br>
{% for i in 1..pageInfo.totalPages %}
{% if i == page %}
{{ i }}
{% else %}
{# Refreshes the component and pushes the new value into the URL #}
<a sprig s-val:page="{{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
</p>
{% endif %}
{% do sprig.pushUrl('?' ~ {
page: page,
categories: catSlugs|length > 0 ? catSlugs|join(',') : null
}|url_encode) %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment