Variable handling commands

assign

This command creates a new variable and assign a specific value to it. If the variable name already exists in the given context, the value will be overwritten. The value can be a constant (of a base type), but you can also assign any kind of types using other Liquid objects. So you can easily implement a counter (e.g. in a for loop) - for the addition of two numbers a filter would be used. Please have a more detailed description on filters  in the appropriate section.

Example:

{% assign red = “#ff0000” %}
<div style="color:{{ red }};">This is a red text</div>
{% assign product_list = collection.products %}
{% for product in product_list %}
  {{ product.title }}
{% endfor %}
{% assign val = 5 %}
{% assign val = val | plus: 1 %}
{{ val }}

Output:

<div style="color:#ff0000;">This is a red text</div>
Tears of Steel
Sintel
Route 66
Star Wreck: In the Pirkinning
Big Buck Bunny
Elephants Dream
6

capture

In order to create complex string variables, it is possible to assign everything in a capture block to a variable, which is specified as the parameter. If the variable already exists, it will be overwritten.

Example:

{% assign test = "variables" %}
{% capture my_text %}
  The text up to the endcapture command will be captured into the string.
  Also {{test}} can be assigned using the capture command.
  {% if test.size > 0 %}
    Also commands can be used within a capture block
  {% endif %}
{% endcapture %}
<div>{{ my_text }}</div>

Output:

<div>The text up to the endcapture command will be captured into the string.
Also variables can be assigned using the capture command.
Also commands can be used within a capture block
</div>	

Read more about other command types: