Vascula v0.1.5

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.

Template
{% if settings.stock > 10 %}In stock{% elsif settings.stock > 0 %}Only {{ settings.stock }} left{% else %}Sold out{% endif %}
Data
{"settings": {"stock": 3}}
Output
Only 3 left

Remember that 0 and "" are truthy. To test for "has something", compare with blank or check the size:

Template
{% if settings.title != blank %}<h2>{{ settings.title }}</h2>{% endif %}{% if settings.items.size > 0 %}{{ settings.items.size }} items{% endif %}
Data
{"settings": {"title": "", "items": ["a"]}}
Output
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.

Template
{% unless settings.signed_in %}<a href="/login">Sign in</a>{% else %}Welcome back{% endunless %}
Data
{"settings": {"signed_in": false}}
Output
<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.

Template
{% case settings.size %}{% when "s", "xs" %}Small{% when "m" or "l" %}Regular{% else %}Large{% endcase %}
Data
{"settings": {"size": "l"}}
Output
Regular

Numbers compare as numbers, so {% when 2 %} matches 2.0.

Template
{% case settings.qty %}{% when 0 %}none{% when 1 %}one{% else %}many{% endcase %}
Data
{"settings": {"qty": 1.0}}
Output
one

A 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.