| 1 | <h2>Comments</h2> |
| 2 | <pre><code>{% comment %}This is a comment{% endcomment %}</code></pre> |
| 3 | |
| 4 | <h2>Control Flow</h2> |
| 5 | |
| 6 | <p>Liquid provides multiple control flow statements.</p> |
| 7 | |
| 8 | <h3>if</h3> |
| 9 | <pre><code> |
| 10 | {% if customer.name == 'kevin' %} |
| 11 | Hey Kevin! |
| 12 | {% elsif customer.name == 'anonymous' %} |
| 13 | Hey Anonymous! |
| 14 | {% else %} |
| 15 | Hi Stranger! |
| 16 | {% endif %} |
| 17 | </code></pre> |
| 18 | |
| 19 | <h3>unless</h3> |
| 20 | |
| 21 | <p>The opposite of <code>if</code> – executes a block of code only if a certain condition is not met.</p> |
| 22 | |
| 23 | <pre><code> |
| 24 | {% unless product.title == 'Awesome Shoes' %} |
| 25 | These shoes are not awesome. |
| 26 | {% endunless %} |
| 27 | </code></pre> |
| 28 | |
| 29 | <h3>case</h3> |
| 30 | |
| 31 | <p>Creates a switch statement to compare a variable with different values. <code>case</code> initializes the switch statement, and <code>when</code> compares its values.</p> |
| 32 | |
| 33 | <pre><code> |
| 34 | {% assign handle = 'cake' %} |
| 35 | {% case handle %} |
| 36 | {% when 'cake' %} |
| 37 | This is a cake |
| 38 | {% when 'cookie' %} |
| 39 | This is a cookie |
| 40 | {% else %} |
| 41 | This is not a cake nor a cookie |
| 42 | {% endcase %} |
| 43 | </code></pre> |
| 44 | |
| 45 | <h3>for</h3> |
| 46 | |
| 47 | <p>Repeatedly executes a block of code.</p> |
| 48 | |
| 49 | <p>break = Causes the loop to stop iterating when it encounters the break tag. <br> |
| 50 | continue = Causes the loop to skip the current iteration when it encounters the continue tag.</p> |
| 51 | |
| 52 | <pre><code> |
| 53 | {% for i in (1..10) %} |
| 54 | {% if i == 4 %} |
| 55 | {% break %} |
| 56 | {% elsif i == 6 %} |
| 57 | {% continue %} |
| 58 | {% else %} |
| 59 | {{ i }} |
| 60 | {% endif %} |
| 61 | {% endfor %} |
| 62 | </code></pre> |
| 63 | |
| 64 | <h3>range</h3> |
| 65 | |
| 66 | <pre><code> |
| 67 | {% for i in (3..5) %} |
| 68 | {{ i }} |
| 69 | {% endfor %} |
| 70 | |
| 71 | {% assign num = 4 %} |
| 72 | {% for i in (1..num) %} |
| 73 | {{ i }} |
| 74 | {% endfor %} |
| 75 | </code></pre> |