Conditionals
#if, elsif, else
if renders its body when the condition is truthy. elsif adds more conditions, tried in order; else renders when none matched.
{% if settings.stock > 10 %}In stock{% elsif settings.stock > 0 %}Only {{ settings.stock }} left{% else %}Sold out{% endif %}{"settings": {"stock": 3}}Only 3 leftRemember that 0 and "" are truthy. To test for "has something", compare with blank or check the size:
{% if settings.title != blank %}<h2>{{ settings.title }}</h2>{% endif %}{% if settings.items.size > 0 %}{{ settings.items.size }} items{% endif %}{"settings": {"title": "", "items": ["a"]}}1 items#unless
unless is if with the condition reversed: its body renders when the condition is not truthy. It may have an else, but no elsif.
{% unless settings.signed_in %}<a href="/login">Sign in</a>{% else %}Welcome back{% endunless %}{"settings": {"signed_in": false}}<a href="/login">Sign in</a>#case, when
case compares one value against several, using equality. The first matching when renders. A when can list several values, separated by commas or or. else renders when nothing matched.
{% case settings.size %}{% when "s", "xs" %}Small{% when "m" or "l" %}Regular{% else %}Large{% endcase %}{"settings": {"size": "l"}}RegularNumbers compare as numbers, so {% when 2 %} matches 2.0.
{% case settings.qty %}{% when 0 %}none{% when 1 %}one{% else %}many{% endcase %}{"settings": {"qty": 1.0}}oneA case needs at least one when. Anything written between {% case %} and the first {% when %} is ignored, so you can lay the tags out on separate lines.
Next: Loops.