Collection page
The starter theme collection section from Sneferu: sort options, filters, a product grid and pagination, with its complete code.
On this page
What you'll build
A product listing with sorting, filters and pagination. The Result below is drawn by the real Sneferu renderer from the code on this page.
Requirements
- Node.js 20 or newer
- A theme made with
npx sneferu init. Every file below is part of it. - The preview running:
npx sneferu dev
Code
Concepts this teaches: Page types, Sorting and filters, Loops, Pagination.
These files come from the starter theme that npx sneferu init creates. The example is sections/collection-main.snf.
<hk-section padding-y="{{ section.settings.padding_y }}" mobile-padding-y="24">
<div class="sf-collection-head">
{% if section.settings.show_title %}
<hk-heading text="{{ collection.title }}" level="h1" size="32" mobile-size="26" />
{% endif %}
{% if section.settings.show_description %}
{% if collection.description %}<hk-rich-text html="{{ collection.description }}" />{% endif %}
{% endif %}
{% if section.settings.show_count %}
<p class="sf-muted">{{ collection.products_count }} {{ t('products_label') }}</p>
{% endif %}
</div>
{% if section.settings.show_sort %}
{% if collection.sort_options %}
<div class="sf-toolbar">
<span class="sf-muted">{{ t('sort_by') }}</span>
{% for o in collection.sort_options %}
<a class="sf-chip" href="{{ o.url }}" aria-current="{{ o.active }}">{{ o.label }}</a>
{% endfor %}
</div>
{% endif %}
{% endif %}
{% if section.settings.show_filters %}
{% if collection.filters %}
<details class="sf-filters">
<summary>{{ t('filters') }}</summary>
{% for f in collection.filters %}
<div class="sf-filter">
<h2 class="sf-filter-name">{{ f.name }}</h2>
{% for v in f.values %}
<a class="sf-chip" href="{{ v.url }}" aria-current="{{ v.active }}">{{ v.label }}</a>
{% endfor %}
</div>
{% endfor %}
</details>
{% endif %}
{% endif %}
{% if collection.products %}
<div class="sf-grid sf-cols-{{ section.settings.columns }}">
{% for p in collection.products limit: 48 %}
{% render 'product-card', product: p, card_style: section.settings.card_style, image_ratio: section.settings.image_ratio, show_price: section.settings.show_price, show_badge: section.settings.show_badge %}
{% endfor %}
</div>
<hk-pagination paginate="{{ paginate }}" />
{% else %}
<div class="sf-empty">
{% if section.settings.empty_image %}
<hk-image src="{{ section.settings.empty_image }}" alt="" width="240" />
{% endif %}
{% if section.settings.empty_title %}
<h2 class="sf-empty-title">{{ section.settings.empty_title }}</h2>
{% endif %}
{% if section.settings.empty_text %}
<p>{{ section.settings.empty_text }}</p>
{% endif %}
{% render 'button', label: section.settings.empty_button_label, href: section.settings.empty_button_link, style: section.settings.empty_button_style %}
</div>
{% endif %}
</hk-section>
<hk-schema>
{
"name": {
"en": "Collection",
"ar": "القسم"
},
"version": 1,
"templates": ["collection"],
"settings": {
"show_title": {
"type": "boolean",
"label": {
"en": "Show title",
"ar": "إظهار العنوان"
},
"default": true
},
"show_description": {
"type": "boolean",
"label": {
"en": "Show description",
"ar": "إظهار الوصف"
},
"default": true
},
"show_count": {
"type": "boolean",
"label": {
"en": "Show product count",
"ar": "إظهار عدد المنتجات"
},
"default": true
},
"show_sort": {
"type": "boolean",
"label": {
"en": "Show sort options",
"ar": "إظهار خيارات الترتيب"
},
"default": true
},
"show_filters": {
"type": "boolean",
"label": {
"en": "Show filters",
"ar": "إظهار الفلاتر"
},
"default": true
},
"columns": {
"type": "range",
"label": {
"en": "Columns",
"ar": "الأعمدة"
},
"min": 2,
"max": 5,
"default": 4
},
"card_style": {
"type": "select",
"label": {
"en": "Card style",
"ar": "شكل الكارت"
},
"options": ["plain", "bordered", "shadow"],
"default": "plain"
},
"image_ratio": {
"type": "select",
"label": {
"en": "Image shape",
"ar": "شكل الصورة"
},
"options": ["1/1", "4/5", "3/4"],
"default": "1/1"
},
"show_price": {
"type": "boolean",
"label": {
"en": "Show price",
"ar": "إظهار السعر"
},
"default": true
},
"show_badge": {
"type": "boolean",
"label": {
"en": "Show sale and sold out badge",
"ar": "إظهار شارة الخصم ونفاد المخزون"
},
"default": true
},
"empty_image": {
"type": "image",
"label": {
"en": "Empty state image",
"ar": "صورة حالة القسم الفاضي"
},
"default": ""
},
"empty_title": {
"type": "text",
"label": {
"en": "Empty state title",
"ar": "عنوان القسم الفاضي"
},
"default": {
"ar": "مفيش منتجات هنا لسه",
"en": "No products here yet"
},
"localized": true,
"maxLength": 80
},
"empty_text": {
"type": "textarea",
"label": {
"en": "Empty state text",
"ar": "نص القسم الفاضي"
},
"default": {
"ar": "جرب قسم تاني أو ارجع للمتجر.",
"en": "Try another collection or go back to the shop."
},
"localized": true,
"maxLength": 200
},
"empty_button_label": {
"type": "text",
"label": {
"en": "Empty state button",
"ar": "زر القسم الفاضي"
},
"default": {
"ar": "كل المنتجات",
"en": "All products"
},
"localized": true,
"maxLength": 30
},
"empty_button_link": {
"type": "link",
"label": {
"en": "Empty state button link",
"ar": "رابط زر القسم الفاضي"
},
"default": "/products"
},
"empty_button_style": {
"type": "select",
"label": {
"en": "Empty state button style",
"ar": "شكل زر القسم الفاضي"
},
"options": ["solid", "outline", "ghost"],
"default": "solid"
},
"padding_y": {
"type": "range",
"label": {
"en": "Vertical padding",
"ar": "المسافة الرأسية"
},
"min": 0,
"max": 160,
"step": 8,
"default": 40
}
}
}
</hk-schema><hk-sections />
<hk-schema>
{
"sections": [
{
"type": "collection-main"
}
]
}
</hk-schema>Download: sections/collection-main.snf, templates/collection.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/collection-main.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
show_title | Show title | boolean | true | |
show_description | Show description | boolean | true | |
show_count | Show product count | boolean | true | |
show_sort | Show sort options | boolean | true | |
show_filters | Show filters | boolean | true | |
columns | Columns | range | 4 | 2 to 5 |
card_style | Card style | select | plain | one of plain, bordered, shadow |
image_ratio | Image shape | select | 1/1 | one of 1/1, 4/5, 3/4 |
show_price | Show price | boolean | true | |
show_badge | Show sale and sold out badge | boolean | true | |
empty_image | Empty state image | image | ||
empty_title | Empty state title | text | {"ar":"مفيش منتجات هنا لسه","en":"No products here yet"} | set per language |
empty_text | Empty state text | textarea | {"ar":"جرب قسم تاني أو ارجع للمتجر.","en":"Try another collection or go back to the shop."} | set per language |
empty_button_label | Empty state button | text | {"ar":"كل المنتجات","en":"All products"} | set per language |
empty_button_link | Empty state button link | link | /products | |
empty_button_style | Empty state button style | select | solid | one of solid, outline, ghost |
padding_y | Vertical padding | range | 40 | 0 to 160 in steps of 8 |
Text keys the code reads with t(). Change the English text in locales/en.json and the Arabic text under the same key in locales/ar.json.
| Key | English text |
|---|---|
products_label | products |
sort_by | Sort by |
filters | Filters |
How it works
- This code uses the tags
hk-section,hk-heading,hk-rich-text,hk-pagination,hk-image,hk-sections. collection.productsis the page of products to show, drawn with{% for p in collection.products limit: 48 %}and the sameproduct-cardsnippet as the grid.collection.sort_optionsandcollection.filtersare lists the platform fills in; the section loops over them to print the links.hk-paginationprints the page links from thepaginatevalue the platform provides.hk-heading,hk-rich-textandhk-imageprint the collection title, description and banner.- Labels such as Sort by and Filters come from
t('sort_by')andt('filters'), so they translate with the locale files.
