In Shopify, snippets are reusable pieces of code that you can include in different templates or sections to avoid duplicating code. Here’s how to create a snippet and call it within a section using render
.
Steps to Create a Snippet in Shopify
- Open Shopify Admin:
- Go to your Shopify Admin dashboard.
- Navigate to the Online Store > Themes:
- In the left-hand sidebar, click Online Store, then Themes.
- Edit Code:
- On the theme you want to edit, click Actions > Edit Code.
- Create a Snippet:
- In the Snippets folder, click Add a new snippet.
- Name your snippet (e.g.,
custom-snippet
), and click Create snippet.
- Write the Code for Your Snippet:
- After creating the snippet, it will open a new file in your code editor. You can add HTML, Liquid, or any custom code you want to reuse across your theme. For example:
<!-- custom-snippet.liquid --> <div class="custom-snippet"> <h2>{{ section.settings.title }}</h2> <p>{{ section.settings.description }}</p> </div>
Save the Snippet:
- After adding your code, click Save to save the snippet.
- After creating the snippet, it will open a new file in your code editor. You can add HTML, Liquid, or any custom code you want to reuse across your theme. For example: