5

Can I write a multiline if statement in Apex/Visualforce HTML?

Something like (definitely pseudocode):

<apex if (condition)>
  <strong>This HTML.</strong>
<apex else>
  <strong>This is the false HTML.</strong>
<apex end>

Update

It seems like the answers here don't work, i.e. neither this:

<apex:outputPanel rendered="{!IF(Lead.Credit__c > 0)}">
  <tr>
    <td>Credit</td>
    <td>{!Lead.Credit__c}</td>
  </tr>
</apex:outputPanel>

Nor this work:

<apex:variable var="v" value="1" rendered="{!IF(Lead.Credit__c > 0)}">
  <tr>
    <td>Credit</td>
    <td>{!Lead.Credit__c}</td>
  </tr>
</apex:variable>

Does it have to be a boolean value?

6

Use like below

<apex:outputtext value="!IF(ISNULL(Eslot.sEvent), 'Free', IF(Eslot.sEvent.Appointment_Type__c == 'Personal', 'Pers', 'Fill'))}"/>

else

<apex:outputPanel rendered = "{! If(m.Name=='sai' ,true,false) }">
</apex:outputPanel>
<apex:outputPanel rendered = "{! If(m.Name=='sam' ,true,false) }">
</apex:outputPanel>
<apex:outputPanel rendered = "{! If(m.Name=='test1' ,true,false) }">
</apex:outputPanel>

Addition to above

If you are using html elements and want to hide based on conditions

<div style= "display: {!If(m.Name=='sai' ,'none','') }">
</div>
<div  style= "display: {! If(m.Name=='sam' ,'none','') }">
</div >
<div  rendered = "display:{! If(m.Name=='test1' ,'none','') }">
</div >
  • That's a single line. I need to fit in multiple lines of HTML. – okay56k Nov 25 '15 at 2:34
  • !IF(ISNULL(Eslot.sEvent), 'Free', IF(Eslot.sEvent.Appointment_Type__c == 'Personal', 'Pers', 'Fill'))} this is multiple conditions – Ratan Paul Nov 25 '15 at 2:36
  • Use rendered tag as I have outlined – Mohith Shrivastava Nov 25 '15 at 2:37
7

Visualforce has IF() function

<strong>
<apex:outputtext value="{!IF(condition, 'This HTML.', 'This is the false HTML.')}"/>
 </strong>

In apex we have if and else statments .You can use rendered tag as well to render based on boolean getter from apex

 <apex:outputPanel rendered="{!booleantrue}"
  <strong>This HTML.</strong>
 </apex:outputPanel>
 <apex:outputPanel rendered="{!NOT(booleantrue)}"
   <strong>This is the false HTML.</strong>
</apex:outputPanel>
4

Use apex:outputPanel

<apex:outputPanel rendered="{!someBooleanFlg}">
    <strong>This HTML.</strong>
</apex:outputPanel>
<apex:outputPanel rendered="{!NOT(someBooleanFlg)}">
    <strong>This is the false HTML.</strong>
</apex:outputPanel>

OR apex:variable

<apex:variable var="v" value="1" rendered="{!someBooleanFlg}">
    <strong>This HTML.</strong>
</apex:variable>
<apex:variable var="v" value="1" rendered="{!NOT(someBooleanFlg)}">
    <strong>This is the false HTML.</strong>
</apex:variable>

The different is apex:outputPanel will output <span> tag and apex:variable will not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.