unless

unless is a Liquid block which only executes its contents when a condition is false. It is a Liquid built-in block. It is the opposite of if, and mostly works in the same way.

Syntax

{% unless condition %}
  Executed if condition evaluates to false.
{% elsif other_condition %}
  Executed if condition is true (so doesn't evaluate), and
  other_condition is true. You can add as many "elsif" blocks as
  you like, or none at all.
{% else %}
  Executed if condition is true, and other_condition is false.
  Just like "elsif", "else" is optional.
{% endunless %}

unless allows certain statements to be executed only if a certain condition is false (or evaluates to false). It it the same as if not. You can check for various conditions using the elsif tag, and if none of the conditions evaluate to true, you can use else to define a default, just like with if. In fact, after the first block, unless operates completely the same as if. End the unless block with the tag endunless.

Examples

{% unless true %}
  This will never be executed.
{% endif %}
 => "true" obviously never evaluates to false.

{% unless user %}
  You're not logged. <a href="{% url_for controller: account, action: login %}">Log in.</a>
{% end %}
 => When the user is logged in, a variable "user" will be available in all
    templates, which is a UserDrop representing the user. If this is not
    the case, we know the user is not logged in.
page_revision: 1, last_edited: 1180813507|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.