Product card
The starter theme product card snippet from Sneferu with its complete code, the language keys it uses and how it is reused by every product list.
On this page
What you'll build
One product as a tile: image, sale or sold-out badge, title and price. 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: Snippets, render, Conditionals, Price display.
These files come from the starter theme that npx sneferu init creates. The example is snippets/product-card.snf.
<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>{% if bind %}
<hk-price amount="{{ product.price }}" compare-at="{{ product.compare_at }}" product="{{ product }}" />
{% else %}
<hk-price amount="{{ product.price }}" compare-at="{{ product.compare_at }}" />
{% endif %}Download: snippets/product-card.snf, snippets/price.snf in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
This snippet has no settings of its own. The section that calls it passes in the product and the options, so the settings live on that section (see the Product grid example).
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 |
|---|---|
sale | Sale |
sold_out_badge | Sold out |
How it works
- This code uses the tags
hk-image,hk-price. - A snippet has no settings of its own. The section that calls it passes values in:
{% render 'product-card', product: p, show_price: true %}. hk-imageshows the product photo with the aspect ratio the caller passes in asimage_ratio.- The badge is plain markup chosen by
{% if product.available %}andproduct.compare_at > product.price: Sale when discounted, Sold out when not available. - The price is another snippet,
price, which printshk-pricewith a compare-at amount when there is one. - Badge text comes from
t('sale')andt('sold_out_badge'), so it follows the visitor's language.
