Product grid
The starter theme featured products section from Sneferu: a collection rendered as a grid of product cards, with its complete code and settings.
On this page
What you'll build
A titled grid of products from a collection, with columns, card style and limit as settings. 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: Collections, Loops, Snippets, Settings, Responsive columns.
These files come from the starter theme that npx sneferu init creates. The example is sections/featured-products.snf.
<hk-section padding-y="{{ section.settings.padding_y }}" mobile-padding-y="32">
<div class="sf-section-head">
{% if section.settings.title %}
<hk-heading text="{{ section.settings.title }}" level="h2" size="32" mobile-size="24" />
{% endif %}
{% if section.settings.link_label %}<a
class="sf-view-all"
href="{{ url(section.settings.link) }}"
>{{ section.settings.link_label }}</a>{% endif %}
</div>
<div class="sf-grid sf-cols-{{ section.settings.columns }}">
{% for p in collection(section.settings.collection).products limit: section.settings.limit %}
{% 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-section>
<hk-schema>
{
"name": {
"en": "Featured products",
"ar": "منتجات مختارة"
},
"version": 1,
"settings": {
"title": {
"type": "text",
"label": {
"en": "Heading",
"ar": "العنوان"
},
"default": {
"ar": "منتجات مختارة",
"en": "Featured products"
},
"localized": true,
"maxLength": 60
},
"collection": {
"type": "collection",
"label": {
"en": "Collection",
"ar": "القسم"
},
"default": "featured"
},
"limit": {
"type": "range",
"label": {
"en": "Number of products",
"ar": "عدد المنتجات"
},
"min": 1,
"max": 24,
"default": 8
},
"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
},
"link_label": {
"type": "text",
"label": {
"en": "Link text (empty hides it)",
"ar": "نص الرابط (فاضي يخفيه)"
},
"default": {
"ar": "عرض الكل",
"en": "View all"
},
"localized": true,
"maxLength": 30
},
"link": {
"type": "link",
"label": {
"en": "Link",
"ar": "الرابط"
},
"default": "/products"
},
"padding_y": {
"type": "range",
"label": {
"en": "Vertical padding",
"ar": "المسافة الرأسية"
},
"min": 0,
"max": 160,
"step": 8,
"default": 56
}
}
}
</hk-schema><article class="sf-card sf-card-{{ card_style }}">
<a class="sf-card-link" href="{{ product.url }}">
<div class="sf-card-media">
<hk-image
src="{{ product.image }}"
alt="{{ product.title }}"
aspect-ratio="{{ image_ratio }}"
sizes="(min-width: 1024px) 25vw, 50vw"
/>
{% if show_badge %}
{% if product.available %}
{% if product.compare_at > product.price %}
<span class="sf-badge">{{ t('sale') }}</span>
{% endif %}
{% else %}
<span class="sf-badge sf-badge-muted">{{ t('sold_out_badge') }}</span>
{% endif %}
{% endif %}
</div>
<h3 class="sf-card-title">{{ product.title }}</h3>
{% if show_price %}{% render 'price', product: product, bind: false %}{% endif %}
</a>
</article>Download: sections/featured-products.snf, snippets/product-card.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/featured-products.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
title | Heading | text | {"ar":"منتجات مختارة","en":"Featured products"} | set per language |
collection | Collection | collection | featured | |
limit | Number of products | range | 8 | 1 to 24 |
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 | |
link_label | Link text (empty hides it) | text | {"ar":"عرض الكل","en":"View all"} | set per language |
link | Link | link | /products | |
padding_y | Vertical padding | range | 56 | 0 to 160 in steps of 8 |
How it works
- This code uses the tags
hk-section,hk-heading,hk-image. collection(section.settings.collection).productsreads the products of the collection the merchant picks (see Lookups).{% for p in ... limit: section.settings.limit %}draws at most that many products.- Each product is drawn by
{% render 'product-card' %}, the snippet shown from the Product card example. hk-headingprints the title only when the merchant sets one, andhk-sectiongives the band its padding.- The column count is a CSS class,
sf-cols-{{ section.settings.columns }}, so the grid stays responsive without code.
