-4

I want to make an output text render specific text based on a condition, for example, a variable is a certain value.

Is there a way to do an if statement in VF?

2 Answers 2

2

Yes, there is.

IF() is one of the functions found on the documentation page for Visualforce functions.

It takes 3 parameters:

  • some expression that returns a boolean value (Name == 'Bob', isActive (no need for testing if a boolean variable is true/false, just use it directly), 1 + 1 > 1, etc...)
  • A value or expression to evaluate if the boolean test from the first parameter ends up being true
  • A value or expression to evaluate if the boolean test from the first parameter ends up being false

Like any other Visualforce thing that isn't a tag, you'll need to use the merge expression syntax. Begin your statement with {!, and end it with }. E.g. {! IF(User.isActive, "active", IF(User.Manager.isActive, "active", "inactive"))}

If you don't want to use the IF() function, you could also likely accomplish this in your controller or controller extension.

public class MyControllerExtension{
    ApexPages.StandardController stdCon;
    // If you don't add {get;set} to the end of a class variable, then it's just
    //   a class variable.
    public String conditionalValue;
    // This is a class "property", and we don't need to explicitly write a setter
    //   or getter.
    // You can still explicitly specify the code to execute when set or get is called
    //   but that's out of the scope of this question
    public String otherValue {get;set}

    public MyControllerExtension(ApexPages.StandardController con){
        this.stdCon = con;
    }

    // Methods that start with "get" (like getMethodName) can be used most places in Visualforce
    //   with {!methodName}
    public String getConditionalValue(){
        // If we've already determined the conditional value, and we haven't
        //   changed any of the things that determine this value, then there isn't
        //   much point in doing the calculation again.
        // Just return the existing value
        if(!String.isBlank(this.conditionalValue)){
            return this.conditionalValue;
        }

        // some logic to determine what conditionalValue should be

        this.conditionalValue = someValue;
        return this.conditionalValue;
    }
}
-1
<apex:outputText rendered="{!Name == 'Bob'}>
<!-- Options which you want to display -->
</apex:outputText>

Name is the apex class variable where we are populating Name value based on condition.

3
  • Are you able to do it for a string == value, not just bool? For example {!if(Name=='bob', true, false)} Commented Oct 24, 2018 at 10:44
  • yes we can do it for the string as well. lets say isAdmin is type of String and have value 'bob', then you can use {!if(Name=='bob', true, false)} in rendered attribute. Commented Oct 24, 2018 at 10:55
  • 1
    You don't need the elaborate if... Just rendered="{!isAdmin}" or rendered="{!name == 'Bob'} will do. Commented Oct 24, 2018 at 11:00

Not the answer you're looking for? Browse other questions tagged .