Software developers tend to adhere to the less is more mentality, particularly when it comes to repetitive work. Why do something twice or more when once is enough?

With Snippets, you can create reusable markup for elements or groups of elements that you find yourself creating frequently. Creating smaller modules of markup also makes it easier to manage and control your theme files.

To create a snippet, open up a theme. You should see a page like this:

In the file tree on the left, click "Add New Snippet". A modal window will appear with an input for the new snippet's name.

Once you've saved your snippet, open it in the editor and add your markup.

After saving that, open the theme file where you'd like to include this snippet. The syntax for including the snippet is:

{% include 'my-snippet' %}

The theme file will now render the markup saved in your snippet wherever you've placed your include statement.

In our example snippet, we might include it in our index.liquid file like this:

And on our store it would appear like this:

Snippets are a convenient way to avoid repetitive markup and to help modularize your theme files. The most common uses for snippets are for things like a footer or header, which rarely change from page to page on your site. You can write all your markup once in your footer.liquid snippet and then include your footer on whatever pages will need it.

N.B. Just like in any other liquid file, you're free to use liquid variables, expressions, and control-flow statements inside snippets.

Did this answer your question?