Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thecatmelon/dbcb9f6c11158c98fdfe to your computer and use it in GitHub Desktop.
Save thecatmelon/dbcb9f6c11158c98fdfe to your computer and use it in GitHub Desktop.
Passing multiple variables to a Snippet for fun and profit

Passing variables through to a Snippet

There are two ways to pass variables to a Snippet, which can be used concurrently or separately:

  • Using with (a single variable)
  • Using a chain of key:value declarations (multiple variables)
{% include 'snippet-name' with value key1:value1 key2:value2 %}

If not including separators is too crazy looking, then feel free to put some commas in:

{% include 'snippet-name' with value, key1:value1, key2:value2 %}

Example: boss.liquid

In a Snippet called boss.liquid, write the following.

<h3>{{ boss }}</h3>
<ul>
  <li>{{ listIem1 }}</li>
  <li>{{ listIem2 }}</li>
</ul>

The tag {{ boss }} will take on the value of anything passed to the Snippet using with. The other tags will take on the value of their matching key.

Used on template pages

In product.liquid, add {% include 'boss' with 'boss things' listIem1: 'accounting' listIem2: 'finance' %} below the heading to get this result:

http://take.ms/PLhDZ

Used in other snippets

In product-loop.liquid add {% include 'boss' with 'easy time' listIem1: 'candy' listIem2: 'popsiciles' %} to get this whole different result:

http://take.ms/gpjYL

Or, add some variable to make it interesting:

{% include 'boss' with collection.title listIem1: product.type listIem2: product.vendor %}

http://take.ms/OZIip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment