Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

if Template Tag


Example

Display a header if the value of the myvar variable is 1:

{% if myvar == 1 %}
  <h1>Hello!</h1>
{% endif %}
Run Example »

Definition and Usage

The if tag allows you to write conditional statements.

Use if statements to output a block of code if a condition is true.

You can use else or elif (short for "else if") to specify what to do when the if condition is false.

Example

Display one heading if myvar is 1, and another if myvar is 2:

{% if myvar == 1 %}
  <h1>Hello!</h1>
{% else %}
  <h1>Greetings!</h1>
{% endif %}
Run Example »

Example

Display a third heading if none of the conditions are true:

{% if myvar == 1 %}
  <h1>Hello!</h1>
{% elif myvar == 2 %}
  <h1>Welcome!</h1>
{% else %}
  <h1>Greetings!</h1>
{% endif %}
Run Example »

Syntax

{% if condition %}
...
{% endif %}

Parameters

Value Description
condition Required. Anything that evaluates to either true or false.

Operators

There are some built-in operators you can use when evaluating if statements:

Variable Description
== is equal to Example »
!= is not equal to Example »
< is less than Example »
<= is less than, or equal to Example »
> is greater than Example »
>= is greater than, or equal to Example »
and condition1 and condition2 must be true Example »
or condition1 or condition2 must be true Example »
in an item must be present in an object Example »
is is the same value as Example »
is not is not the same value as Example »
not in is not in Example »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.