Skip to main content

Snippets

Snippets are reusable pieces of Liquid code that help maintain consistency and reduce duplication across your theme.

Overview

  • Not visible in the theme editor
  • Ideal for code that’s repeated throughout your theme
  • Can be thought of as “components” or “partials”
  • Can be created, deleted, or renamed as needed

Creating Snippets

Snippets are stored in the snippets/ directory with a .liquid extension. For example:

Using Snippets

To include a snippet in a section or another snippet, use the render tag:

Passing Variables to Snippets

You can pass variables to snippets when rendering them:
Inside the snippet, these variables are available as local variables:

Default Values

You can provide default values for snippet variables using the default filter:

Common Snippet Use Cases

UI Components

  • Buttons
  • Cards
  • Navigation elements
  • Progress bars

Content Patterns

  • Course listings
  • Module layouts
  • Lesson previews
  • User information displays

Example: Image Snippet

A snippet for consistently rendering images with optional lazy loading:
Usage:

Best Practices

  • Create snippets for components used in multiple places
  • Pass variables to snippets for flexibility
  • Use consistent naming conventions
  • Keep snippets focused on a single responsibility
  • Document the expected variables for each snippet
  • Use default values for optional parameters