When building custom Shopify themes or customizing an existing one, developers often repeat the same chunks of code across product cards, product detail pages, collection views, and more. Over time, these repetitions make the theme harder to manage, slow to maintain, and prone to bugs.

That’s where Shopify snippets come in.

Snippets in Shopify are modular .liquid files that allow you to reuse blocks of code wherever needed—without rewriting or duplicating them. Whether it’s a discount badge, a trust icon bar, or a custom logic block like showing metafields or “Backordered” messages, snippets help you centralize logic, reduce theme bloat, and simplify updates.

In this blog post, we’ll walk you through:

  • What a Shopify snippet is

  • How to create your first one step by step

  • How to reuse it in multiple places

  • Best practices for working with snippets in a scalable Shopify theme

By the end, you’ll be ready to create clean, reusable, and professional-level snippets that can save you hours of work across projects.

🔍 What is a Shopify Snippet?

A Shopify snippet is a small, reusable .liquid file that lives in your theme’s snippets/ directory. Think of it like a partial template—something you can call into other files like sections, templates, or blocks to keep your code clean and organized.

📂 Where Snippets Live

Snippets are stored in:

bash
/snippets/

For example:

bash
/snippets/price-discount.liquid
/snippets/trust-badges.liquid
/snippets/product-icon.liquid

🧱 Why Use Snippets?

Let’s say you’ve written a block of code that calculates the discount percentage from compare_at_price and price. If you manually paste that logic into:

  • Product cards

  • Product detail pages (PDP)

  • Featured product sections

  • Sliders or custom liquid blocks

…any change to the logic means updating it in multiple places.

Instead, create a snippet once and reuse it everywhere using the {% render %} tag. Now you can change it in one place, and the update reflects sitewide—saving time, reducing errors, and making your codebase DRY (Don’t Repeat Yourself).

✅ When Should You Use a Snippet?

Use snippets when:

  • The code is repeated in 2+ places

  • The logic is self-contained (e.g., badges, tooltips, icons, conditional text)

  • You want to allow settings or values to be passed in (e.g., color, product)

You shouldn’t use snippets when:

  • You need to include schema or theme settings (use sections instead)

  • You’re working with layout-based logic (e.g., full page templates)

🛠️ Creating Your First Snippet (Step-by-Step)

In this section, we’ll create a simple Shopify snippet that calculates and displays a discount badge on a product—something you can reuse across your entire store.

🎯 Goal:

We want to display a badge like this:

“20% OFF”
…only when compare_at_price is higher than the actual price.


📁 Step 1: Create the Snippet File

Go to your Shopify admin:

  1. Click Online StoreThemes

  2. Click Edit code on your active theme

  3. Under the Snippets folder, click Add a new snippet

  4. Name it:

    price-discount.liquid

💡 Step 2: Write the Snippet Logic

Here’s a basic version of the snippet you can paste into price-discount.liquid:

liquid
{% comment %}
Reusable snippet to show discount percentage if product has a compare_at_price.
Params:
- product: required, product object
- class: optional, custom CSS class
- badge_color: optional, custom background color
{% endcomment %}
{% liquid
assign class = class | default: ‘discount-badge’
assign badge_color = badge_color | default: ‘#D93F21’

assign original_price = product.compare_at_price | default: 0
assign current_price = product.price | default: 0

if original_price > current_price
assign discount = original_price | minus: current_price | times: 100 | divided_by: original_price | round
endif
%}

{% if discount and discount > 1 %}
<span class=”{{ class }}” style=”background-color: {{ badge_color }}”>
{{ discount }}% OFF
</span>
{% endif %}


🔧 Step 3: Customize Styling (Optional)

You can target .discount-badge in your CSS or add inline styles if needed. Example:

css
.discount-badge {
color: #fff;
font-weight: bold;
padding: 4px 8px;
border-radius: 4px;
font-size: 0.8rem;
display: inline-block;
}

That’s it! You now have a fully working, reusable discount badge snippet.

Next, we’ll learn how to use this snippet in your theme with real-world examples.

🔄 How to Use Snippets in Your Theme

Now that you’ve created your first snippet (price-discount.liquid), it’s time to use it across your theme—on product grids, product pages, featured collections, or any custom section.

🧩 Use the {% render %} Tag

To insert your snippet anywhere in your theme files, use Shopify’s {% render %} tag. This is the modern and recommended way to include snippets (instead of the older {% include %} tag).

🧪 Basic Example

Let’s say you’re inside a product loop on your collection page or homepage product card:

liquid
{% render 'price-discount', product: product %}

This line will inject the price-discount.liquid snippet and pass the current product object to it.


🎨 Passing Additional Settings

You can also pass optional parameters like a custom class or background color:

liquid
{% render 'price-discount', product: product, class: 'badge-sm', badge_color: '#0E9F6E' %}

Now your badge might look like a green “15% OFF” pill styled with the badge-sm class.


🧠 Where to Use Snippets

You can use this snippet in multiple theme files:

✅ Collection Page

liquid
<!-- In product-card.liquid or similar -->
<div class="product-tile">
{% render 'price-discount', product: product %}
</div>

✅ Product Detail Page

liquid
<!-- In main-product.liquid -->
<div class="price-block">
{% render 'price-discount', product: product, badge_color: '#B63C48' %}
</div>

✅ Homepage Product Sliders

liquid
<!-- In a section like featured-collection.liquid -->
{% for product in section.settings.collection.products %}
<div class="slide-item">
{% render 'price-discount', product: product %}
</div>
{% endfor %}

✅ Custom Liquid Blocks (via Shopify Editor)

If you’re using Custom Liquid blocks in the Online Store Editor:

liquid
{% render 'price-discount', product: all_products['kurta-set-01'] %}

📌 Note: all_products['handle'] can be used if you’re outside a loop and need to reference a specific product.


✅ With one line, you’re injecting powerful logic into any layout—without rewriting the code again.

Best Practices for Snippet Development

Creating reusable snippets is one thing—but creating scalable, error-proof, and developer-friendly snippets is what separates good themes from great ones. Below are some best practices you should follow when building snippets in Shopify.


1. 🧼 Use the render Tag (Not include)

Always use {% render 'snippet-name' %} instead of the deprecated {% include %} tag.
Why?

  • render creates its own variable scope (no conflicts)

  • More reliable in Shopify Online Store 2.0

  • Future-proof and required in Theme Store submissions


2. ⚙️ Pass Only What You Need

Instead of relying on global variables (like product), pass them explicitly:

liquid
{% render 'price-discount', product: product, badge_color: '#F43F5E' %}

This makes your snippet more modular and testable.


3. 📦 Handle Missing or Null Values Gracefully

Not all products will have compare_at_price, metafields, or optional attributes. Use Liquid filters like default, blank, or nil checks:

liquid
assign original_price = product.compare_at_price | default: 0

And wrap your output safely:

liquid
{% if discount and discount > 0 %}
<span>...</span>
{% endif %}

4. 🧠 Use Defaults Inside the Snippet

Always set default values inside the snippet to prevent theme crashes if optional values aren’t passed:

liquid
assign class = class | default: 'badge'
assign badge_color = badge_color | default: '#000'

5. 🧩 Keep Snippets Small and Purpose-Driven

Each snippet should do one job well. Avoid bloated logic that combines unrelated functionality.

❌ Bad: A snippet that handles price, stock status, and shipping message
✅ Good: A snippet that only shows a discount badge


6. 📝 Document Your Snippets

Add a comment block at the top of each snippet:

liquid
{% comment %}
Shows discount % based on compare_at_price and price.
Params:
- product: Product object
- class: optional CSS class
- badge_color: optional background color
{% endcomment %}

This helps teams (and future you) understand what’s going on.


7. 🪄 Keep Styling External or Dynamic

Avoid inline CSS when possible. Instead, allow class overrides or use existing utility classes like Tailwind, Bootstrap, or theme-defined classes.

liquid
<span class="{{ class }}" style="background-color: {{ badge_color }}">

8. 🧪 Test in All Contexts

Test your snippet in:

  • Product cards

  • Mobile and desktop layouts

  • Custom Liquid blocks

  • Different product types (with and without compare_at_price)

This ensures it’s bulletproof before production.


Following these practices will help you build professional-grade Shopify themes that are modular, maintainable, and scalable.

🚀 Advanced Snippet Tips (Optional)

Once you’re comfortable building basic Shopify snippets, you can start leveraging more advanced techniques to make them even more powerful and flexible. These tips are especially helpful for large themes, dynamic features, or collaborative development.


1. 🧩 Nesting Snippets

Yes—you can render a snippet inside another snippet. This helps break large UI components into smaller parts.

Example:
Inside product-badge.liquid, you can reuse icon-star.liquid like this:

liquid
<span class="badge">
{% render 'icon-star' %}
Best Seller
</span>

Great for icons, loaders, or small UI components.


2. 📥 Capturing Output from Snippets

You can use {% capture %} to grab the rendered output of a snippet and manipulate it.

liquid
{% capture discount_badge %}
{% render 'price-discount', product: product %}
{% endcapture %}
{% if discount_badge != blank %}
<div class=”badge-wrapper”>{{ discount_badge }}</div>
{% endif %}

This is helpful for conditional rendering or applying wrappers only if content exists.


3. 🔁 Dynamic Rendering in Loops

Inside for loops (e.g., collection pages), you can dynamically call snippets for each product:

liquid
{% for product in collection.products %}
{% render 'price-discount', product: product %}
{% endfor %}

Just make sure your snippet is optimized for performance.


4. 📦 Using Metafields Inside Snippets

Snippets work great with metafields. Just pass the required object:

liquid
{% render 'product-icons', product: product %}

Inside product-icons.liquid, access metafields like:

liquid
{% if product.metafields.custom.icon_type == 'new' %}
<span class="icon-new">New Arrival</span>
{% endif %}

5. 🧩 Reusable Component Snippets

Create reusable UI snippets like:

  • button-primary.liquid

  • section-heading.liquid

  • tooltip.liquid

  • custom-form.liquid

This improves visual consistency across your site.


6. 🌍 Use Translations with t Filter

If your store is multilingual or supports localization, use translation keys:

liquid
{{ 'products.discount_label' | t }}

This allows Shopify’s translation system to pick up content from your locales/*.json files.


7. 🗂️ Organize Snippets by Category

In larger themes, group snippets by prefix:

  • product-*.liquid

  • cart-*.liquid

  • icon-*.liquid

  • ui-*.liquid

Example:

css
snippets/
└── product-badge.liquid
└── product-icons.liquid
└── icon-check.liquid
└── ui-button.liquid

This makes file navigation easier for teams.


These advanced practices help you turn a basic snippet library into a powerful component system—just like modern frontend frameworks.

⚠️ Common Mistakes to Avoid When Using Snippets

Even though snippets are powerful, misusing them can lead to broken layouts, buggy logic, or difficult-to-maintain codebases. Below are the most common mistakes developers make when working with Shopify snippets—and how to avoid them.


1. ❌ **Using {% include %} Instead of {% render %}

Shopify officially recommends using {% render %} because it:

  • Creates its own variable scope

  • Prevents variable conflicts

  • Is faster and safer

Wrong:

liquid
{% include 'price-discount' %}

Correct:

liquid
{% render 'price-discount', product: product %}

2. ❌ Adding Schema in a Snippet

Snippets cannot contain schema or settings. If you try to add:

liquid
{% schema %}
{
...
}
{% endschema %}

…it will break your theme.

✅ Use sections for schema-driven customization.
Snippets are for logic and display only.


3. ❌ Forgetting to Pass Required Variables

If your snippet depends on product, collection, or variant—always pass it:

Wrong:

liquid
{% render 'price-discount' %}

Correct:

liquid
{% render 'price-discount', product: product %}

Failure to do so can result in undefined errors or blank output.


4. ❌ Hardcoding Logic Instead of Making It Reusable

Don’t tie your snippet to one specific product or style.

Avoid:

liquid
<span style="background: red;">20% OFF</span>

✅ Instead, allow dynamic input:

liquid
{% render 'price-discount', product: product, badge_color: '#F87171' %}

This makes it reusable across themes or clients.


5. ❌ Overloading Snippets with Too Much Responsibility

Snippets should follow the single-responsibility principle.

Don’t do this:

  • Show discount

  • AND check stock

  • AND show metafield icons

  • AND render wishlist button

Instead:

  • price-discount.liquid

  • product-stock.liquid

  • wishlist-icon.liquid

Keep things modular.


6. ❌ Using Global Variables Unnecessarily

Avoid relying on global objects like product, cart, or settings when it’s better to pass them directly. This ensures your snippet works consistently, even inside a loop or alternate context.


7. ❌ Not Testing Snippets in Multiple Use Cases

Your snippet may work on a product card—but break:

  • Inside a featured product section

  • On a product with missing metafields

  • On a mobile layout

✅ Always test with:

  • Different screen sizes

  • Products with/without compare_at_price

  • Custom liquid blocks


Avoiding these mistakes will help you build robust, clean, and developer-friendly Shopify themes.

🧑‍💻 Snippet Maintenance and Team Tips

As your Shopify theme grows—or as your team expands—managing snippets properly becomes essential. Here are key tips for maintaining snippet quality, improving collaboration, and ensuring your theme remains scalable.


1. 🗃️ Use Clear and Consistent Naming

Avoid vague names like box.liquid or logic.liquid. Instead, name snippets by their function and context.

Good examples:

  • price-discount.liquid

  • product-icons.liquid

  • cart-upsell-banner.liquid

  • ui-button.liquid

This helps developers instantly understand the purpose of each snippet.


2. 📝 Document Your Snippets

Always add inline comments at the top of your snippets describing:

  • What it does

  • What variables are expected

  • Optional parameters

Example:

liquid
{% comment %}
Renders a % OFF badge.
Params:
- product: Required
- class: Optional CSS class
- badge_color: Optional hex value
{% endcomment %}

This helps teammates onboard quickly—and makes debugging easier months later.


3. 🧪 Test Snippets in Multiple Contexts

Before finalizing a snippet, test it:

  • On different sections (cards, PDP, sliders)

  • With multiple product types (with and without metafields)

  • On mobile and desktop

  • With missing or empty data

Make testing part of your snippet checklist.


4. 📁 Organize Snippets by Category

If your theme has 20+ snippets, grouping helps.

Structure them with naming conventions like:

markdown
product-*.liquid
cart-*
.liquid
icon-*.liquid
ui-*
.liquid

Or maintain a simple developer index in Notion, Google Sheets, or a README.md in your GitHub repo.


5. 🔄 Make Reuse Intentional

If you find yourself copy-pasting the same logic into different snippets—refactor it out into a smaller shared snippet.

For example:

  • A calculate-discount.liquid snippet used inside:

    • price-discount.liquid

    • bundle-card.liquid

    • cart-discount-info.liquid

This reduces redundancy and improves maintainability.


6. 👥 Collaborate Using Git or Shopify CLI

If you’re working with a team:

  • Use Shopify GitHub integration to manage code versions

  • Or use Shopify CLI 3.x to push/pull changes

  • Add snippet-specific PRs (e.g., “Add trust badge snippet for PDP”)

Version control is critical when working on live stores or theme submissions.


7. 🚀 Create a Snippet Library or Component System

If you’re building multiple stores or plan to sell your theme:

  • Maintain a library of snippets as building blocks

  • Treat it like a design system (just like Figma for UI)

Examples:

  • ui-button.liquid

  • section-heading.liquid

  • form-field.liquid

  • icon-check.liquid

This turns your theme into a powerful, consistent platform for rapid development.


Well-maintained snippets make your theme developer-friendly, client-safe, and future-proof.

🧾 Conclusion

Snippets are one of the most powerful tools in the Shopify theme developer’s toolkit. By turning repeatable logic into clean, reusable modules, you create a foundation for scalable, maintainable, and professional-grade themes.

Whether you’re building a discount badge, a trust icon strip, or a custom metafield renderer—writing that feature as a snippet allows you to:

  • Write once, use everywhere

  • Update centrally without chasing bugs

  • Collaborate easily with other developers

  • Keep your theme DRY and modular

To summarize, here’s your snippet success checklist:
✅ Use {% render %}, not {% include %}
✅ Pass variables explicitly
✅ Handle fallbacks and missing data
✅ Keep your logic focused
✅ Document everything
✅ Test in multiple contexts
✅ Organize and scale your snippet library

As your Shopify development matures, snippets become more than just helpers—they become your component system, making every new feature faster and more reliable.

Looking for an expert Shopify freelance developer from India to build a high-performance store with clean, reusable code snippets? Let’s connect—I’d be happy to help!