-
Notifications
You must be signed in to change notification settings - Fork 119
Variable Assignment
harrydeluxe edited this page May 13, 2012
·
2 revisions
You can store data in your own variables, to be used in output or other tags as desired.
The simplest way to create a variable is with the assign
tag, which has a pretty straightforward syntax:
{% assign name = 'freestyle' %}
{% for t in collections.tags %}
{% if t == name %}
<p>Freestyle!</p>
{% endif %}
{% endfor %}
Another way of doing this would be to assign true / false
values to the variable:
{% assign freestyle = false %}
{% for t in collections.tags %}{% if t == 'freestyle' %}
{% assign freestyle = true %}
{% endif %}{% endfor %}
{% if freestyle %}
<p>Freestyle!</p>
{% endif %}
If you want to combine a number of strings into a single string and save it to a variable, you can do that with the capture
tag. This tag is a block which "captures" whatever is rendered inside it, then assigns the captured value to the given variable instead of rendering it to the screen.
{% capture attribute_name %}
{{ item.title | handleize }}-{{ i }}-color
{% endcapture %}
<label for="{{ attribute_name }}">Color:</label>
<select name="attributes[{{ attribute_name }}]" id="{{ attribute_name }}">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
</select>