if
if is a Liquid block which only executes its contents when a condition is matched. It is a Liquid built-in block.
Syntax
{% if condition %}
Executed if condition evaluates to true.
{% elsif other_condition %}
Executed if condition is false, but other_condition evaluates to true.
You can add as many "elsif" blocks as you like, or none at all.
{% else %}
Executed if none of the above conditions evaluate to true.
Just like "elsif", "else" is optional.
{% endif %}
if allows certain statements to be executed only if a certain condition is true (or evaluates to true). You can check for various conditions using the elsif tag1, and if none of the conditions evaluate to true, you can use else to define a default. End the if block with the tag endif.
The condition can be one of the following:
- A single variable. In this case, the expression evaluates to true if the variable exists (is not nil) and is not false. (Note: empty strings evaluate to true.)
- Two variables separated by an operator. You can compare two variables using the operators == (equals), != and <> (does not equal), < (is less than), > (is greater than), <= (is less than or equal to) and >= (is more than or equal to). (TODO: Explain about comparing with symbols and respond_to?.)
Examples
{% if true %}
This will always be executed.
{% endif %}
=> "true" obviously always evaluates to true.
{% if entry.get_comment_count == 0 %}
There are absolutely no comments on this entry. Zip, zero, nada!
{% elsif entry.get_comment_count == 1 %}
One person commented on this entry.
{% else %}
There are {{ entry.get_comment_count }} comments on this entry.
{% endif %}
=> This will display different messages depending on how much comments
were written on your entry.
{% if user %}
You're logged in as {{ user.nickname }}.
{% end %}
=> When the user is logged in, a variable "user" will be available in all
templates, which is a UserDrop representing the user.
page_revision: 4, last_edited: 1180712658|%e %b %Y, %H:%M %Z (%O ago)





