Introduction
Snippets are reusable Liquid components that can be included throughout your Horizon theme. They help maintain consistent functionality and appearance across different sections and templates.What are Snippets?
Snippets are small, reusable pieces of Liquid code stored in thesnippets/ directory. They’re designed to be included using the {% render %} tag, which creates an isolated scope for the snippet.
Key Features
- Isolated Scope: Snippets rendered with
{% render %}have their own variable scope - Parameterized: Accept parameters for customization
- Reusable: Can be used across multiple sections, blocks, and templates
- Modular: Encourage separation of concerns and maintainability
Common Snippet Categories
The Horizon theme includes several categories of snippets:Product Components
product-card.liquid- Display product information in card formatprice.liquid- Format and display product pricingproduct-media.liquid- Render product images and videosvariant-swatches.liquid- Display variant selection swatchesquick-add.liquid- Quick add to cart functionality
Cart Components
cart-summary.liquid- Display cart totals and checkout buttonscart-products.liquid- Render cart line itemsadd-to-cart-button.liquid- Standalone add to cart button
UI Components
button.liquid- Styled button linksicon.liquid- SVG icon renderingimage.liquid- Responsive image renderingmedia.liquid- Media element wrapper
Form Components
checkbox.liquid- Styled checkbox inputsquantity-selector.liquid- Product quantity inputsearch.liquid- Search input field
Layout Components
section.liquid- Section wrapper with stylinggroup.liquid- Group multiple elementsdivider.liquid- Visual dividersoverlay.liquid- Overlay effects
Utility Snippets
format-price.liquid- Price formatting helpergap-style.liquid- Spacing utilitiessize-style.liquid- Size utilitiescolor-schemes.liquid- Color scheme management
Usage
Snippets are included using the{% render %} tag:
Example
Documentation Structure
Parameters are documented using the{%- doc -%} tag at the top of each snippet file:
Best Practices
Use render instead of include
Use render instead of include
Always use
{% render %} instead of {% include %} to maintain proper variable scoping and prevent unintended side effects.Document your parameters
Document your parameters
Always include
{%- doc -%} blocks at the top of snippets to document accepted parameters and their types.Set sensible defaults
Set sensible defaults
Use the
default filter to provide fallback values for optional parameters:Keep snippets focused
Keep snippets focused
Each snippet should have a single, well-defined purpose. Break complex functionality into smaller, composable snippets.
Next Steps
Product Card
Learn how to use the product card snippet
Cart Summary
Display cart totals and checkout options
Buttons
Implement styled buttons and actions
Sections
Explore how sections use snippets