Passing a parameter

Assign variables when you retrieve your snippet.

Parameters assign variable values when you retrieve a snippet:

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

Array and object parameters are supported.

Input
{% for product in products %}
  {{ product.name }} for {{ product.price }}!
{% endfor %}
Output
<!-- /api/snippets/1?params={"products":[{name:"Hat",price:1},{name:"Shirt",price:2}]} -->
Hat for 1!
Shirt for 2!

Precedence

The assign tag takes precendence over parameters, and parameters takes 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
<!-- /api/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