5

I had used c:if, c:when JSTL tags in jsp. But I don't know if anything similar is available for visual force pages. just for example I am providing sample code for jsp. --

    <h1>A Demo conditional section code</h1>
    <c:choose>
       <c:when test="${param.colorField == 'red'}">
         <table border="0" width="150" height="50" bgcolor="#ff0000">
           <tr><td>It is red</td></tr>
         </table>
      </c:when>
      <c:when test="${param.colorField == 'blue'}">
       <table border="0" width="150" height="50" bgcolor="#0000ff">
         <tr><td>It is blue</td></tr>
      </table>
    </c:when>
    <c:when test="${param.colorField == 'green'}">
        <table border="0" width="150" height="50" bgcolor="#00ff00">
          <tr><td>Green table</td></tr>
        </table>
    </c:when>
    <c:otherwise>
      <table border="0" width="150" height="50" bgcolor="#000000">
        <tr><td>No colour changed</td></tr>
       </table>
    </c:otherwise>
</c:choose>
<br/>
and other codes....

I am missing this kind of page block preparation in vf pages.

Plymouth Rock
  • 472
  • 2
  • 6
  • 19

4 Answers4

7

What I have found that we can use outputpanel (<apex:outputpanel>) for any block and use the rendered attribute to handle the condition for loading it.

<h1>A Demo conditional section code</h1>
    <apex:outputpanel rendered="{!param.colorField == 'red'}">
         <table border="0" width="150" height="50" bgcolor="#ff0000">
           <tr><td>It is red</td></tr>
         </table>
    </apex:outputpanel>
    <apex:outputpanel rendered="{!param.colorField == 'blue'}">
       <table border="0" width="150" height="50" bgcolor="#0000ff">
         <tr><td>It is blue</td></tr>
      </table>
    </apex:outputpanel>
    :
    :
and other codes....
Plymouth Rock
  • 472
  • 2
  • 6
  • 19
4

Same concept as the other answers on here, but you can use the rendered attribute on a PageBlock to render that block or not:

<apex:pageBlock rendered="{!object.Color == 'red'}">
    it is red
</apex:pageBlock>
<apex:pageBlock rendered="{!object.Color == 'blue'}">
    it is blue
</apex:pageBlock>
<apex:pageBlock rendered="{!object.Color == 'green'}">
    it is green
</apex:pageBlock>
willard
  • 252
  • 3
  • 10
1

In visualforce you can use some logical operators and functions. Explanation here

You need "Logical Functions" list, same code that you provide, in VF must look like this:

{!IF(salary<=0, "Salary is very low to survive.", "No comment sir")} 
Brane
  • 2,781
  • 2
  • 31
  • 51
nickzenko
  • 36
  • 2
  • Ok. In that case my given sample is too simple to state the complexity I wanted to mention. Say for a condition I want to show a set of data in a tabular form. Otherwise say I would have print a paragraph. Then what? I want to switch among a code blocks containing lots of html tags and values etc. – Plymouth Rock Apr 07 '15 at 06:28
  • 1
    For switch code blocks you can use rendered atribute ... ... – nickzenko Apr 07 '15 at 06:48
0

You need to put the logic in the controller (where most people say it belongs anyways). Your VF would look like:

<table border="0" width="150" height="50" bgcolor="{!bgColorVar}">

And in your controller, define your logic in the getter:

public string bgColorVar{
    get{ 
        //logic
    }
    set;
}
raffters
  • 196
  • 4