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?

closed as off-topic by David Reed, glls, battery.cord, Dave Humm, Mohith Shrivastava Oct 25 '18 at 0:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – David Reed, glls, battery.cord, Dave Humm, Mohith Shrivastava
If this question can be reworded to fit the rules in the help center, please edit the question.


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
            return this.conditionalValue;

        // some logic to determine what conditionalValue should be

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

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

  • Are you able to do it for a string == value, not just bool? For example {!if(Name=='bob', true, false)} – Alexander Atkinsoon Oct 24 '18 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. – HowToDoItInSalesforce Oct 24 '18 at 10:55
  • 1
    You don't need the elaborate if... Just rendered="{!isAdmin}" or rendered="{!name == 'Bob'} will do. – rael_kid Oct 24 '18 at 11:00

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