10

Is there any option for using IF-ELSE conditioning in HTML tags

 <if true>  do something   </if>  
 <else>     do something   </else>

10 Answers 10

15

There is, but it's really only used in IE to distinguish between different versions:

<!--[if IE6]>
    Things here!
<![endif]-->
Sign up to request clarification or add additional context in comments.

Comments

9

Not to be pedantic, but HTML is a markup language, and isn't useful for conditional logic.

That being said, it sounds like what you're looking for is a bit of javascript. If you could add a bit more detail to your question, I could elaborate on how you could use javascript to do tasks with conditional logic.

3 Comments

I don't think you're being pedantic ... spot on - HTML for markup only.
Very late with this remark, but isn't PHP the go-to solution for if/else cases? If a user doesn't have javascript enabled in their browser or the javascript isn't cross-browser (or has errors) you could end up with unexpected, unwanted results. PHP doesn't do client-side evaluation so the end result is more predictable. I love javascript, but it shouldn't be a go-to solution for everything.
Well, to be honest, everything is late about that. Every major browser has strong javascript support. Sure, javascript isn't always universal, but there is normally some strange workaround for every browsing engine that needs it.
5

HTML was designed for document layout so the noscript and noframes are about as close as HTML gets to handling conditionals. You could conceivably approach this problem with javascript.

<div id='if-part' style='visibility: hidden;'>do something</div>
<div id='else-part' style='visibility: hidden'>do something</div>

<script>
    var node;
    if(true) {
        node = document.getElementById('if-part');
    }
    else {
        node = document.getElementById('else-part');
    }
    node.style.visibility = 'visible';
</script>

of course this only works if the client has javascript turned on.

2 Comments

What about the <noscript> tag? Seems like it might work for the else condition.
true, but only if javascript is disabled client side.
4

Conditional rendering of HTML is not a new concept, but it cannot be done using HTML exclusively. You would need to use either client side scripting or server side code to provide the conditional logic that would render your HTML accordingly.

Comments

3

As has been said in other posts, HTML does not support conditional logic. You have two choices here:

1) Generate the HTML dynamically using technologies such as PHP or XSLT

2) Modify the HTML DOM after the fact using Javascript

Comments

3

With acknowledgement to Kevin Loney and his javaScript solution, we should also consider CSS conditionals. If the conditional is based on browser-sensing/responsive design, then javaScripting can be bypassed:

<div class='if-part'>show something</div>
<div class='else-part'>show something</div>

"Show something" in the sense that you would merely hide the condition which does not apply. In the example below, "if-part" is shown on mobile devices only. "else-part" is shown on larger screens.

@media screen and (max-width:767px){
    .if-part{display:initial;}
    .else-part{display:none}
}
@media screen and (min-width:768px){
    .if-part{display:none;}
    .else-part{display:initial}
}

This answer offers even more CSS options to consider.

Comments

2

Have you guy's ever coded an email? All of your java script is stripped by google. Furthermore, gmail on android does not support media queries, and different versions of outlook have their own quirks. You have no choice but to use conditional HTML if you want to emails that render well on a variety of email clients.

This is much like the second example:

<!--[if gte mso 9]>
    <style type="text/css">
    /* Your Outlook-specific CSS goes here. */
    </style>
<![endif]-->

However, if you are not going through an email client I would have to agree with everyone else and say you should use Java Script.

Comments

-1

These days, you can use frameworks like React.js and the code will look something like this:

{(condition)?(<div>True Condition</div>):(<div>False Condition</div>)}

The Javascript runs on the client side, therefore, first evaluates the condition and then renders the HTML object as per the condition.

Or, you can explore framework like Next.js if you want to do the javascript work on the server side.

Comments

-2

Application logic belongs on the server. The place to do stuff like this is on the server. There are a number of html template languages that will accomplish this. This processes the conditionals before sending the data to the client. The client data will be regular html, without conditionals. Django or Flask would be examples of a popular template engines.

3 Comments

What makes you think that all of this is pure server-logic? Nowadays, a lot of stuff can be pre-processed in the browser, like conditionally show one form field if another has a specific value
All that needs to be done on the server, not dividing up your logic in 20 places
Please add all clarification to your answer by editing it. I though that stuff like React, Angular, or similar frameworks are industry standard, but maybe I'm working wrong since years
-5
    ***Syntax***

{% if condition1 %} lines of code

{% elif condition2 %} lines of code

{% else %} lines of code

{% endif %}

    ***Example***

role='Manager'

{% if role=='Admin' %} Hello admin

{% elif role=='Manager' %} Hello Manager

{% else %} You are normal user

{% endif %}

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
not html and you didn't say what language you were using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.