Passing a parameter

Assign variables when you retrieve your snippet.

Parameters assign variable values when you render a snippet:

Input
The sky is {{ color }}.
Output
<!-- /snippets/1?params={"color":"blue"} -->
The sky is blue.

Array parameters are supported.

Input
{% for name in names %}
  Hi {{ name }}!
{% endfor %}
Output
<!-- /snippets/1?params={"names":["Jack","Nancy","Harry"]} -->
Hi Jack!
Hi Nancy!
Hi Harry!

Object parameters are also supported.

Input
Hi {{ person.name.first }}!
Output
<!-- /snippets/1?params={"person":{"name":{"first":"Captain","last":"Picard"}}} -->
Hi Captain!

Precedence

The assign tag takes precendence over parameters, and parameters take precedence over the default filter. If a parameter is missing, the variable is not displayed. If extra parameters are passed, they're discarded.

Input
{% assign foo = "hat" %}
{{ foo }}
{{ bar }}
{{ baz | default: "shoes" }}
{{ qux }}
Output
<!-- /snippets/1?params={"foo":"shirt","bar":"pants","corge":"socks"} -->
hat
pants
shoes

In the example above:

  • the value of foo was "hat", because assign takes precedence over parameters;
  • the value of bar was set by the passed parameters;
  • the value of baz was never assigned, but the default filter printed "shoes" to the page;
  • the value of qux was never assigned or passed by parameter, so nothing was printed to the page; and,
  • the parameter corge was passed but never used and ignored.

Next steps

Now that you know how to pass parameters, learn how to apply filters to change a variable's output.

Applying a filter Ask a question