Component Index contrib:FormConditional

contrib:Choose
org.apache.tapestry.contrib.components.Choose
Non Visual Component
 

Description
The contrib:Choose component is used in conjunction with contrib:When and contrib:Otherwise components to express mutually exclusive conditional tests.

The contrib:Choose component must contain one or more contrib:When components and can contain only one optional contrib:Otherwise component (which must occur after all of the contrib:When components). If the contrib:Choose component only contains one contrib:When element, then for all practical purposes, it behaves just like the Conditional component. When faced with three or more choices, this component behaves like a switch/case/default statement.

Each contrib:When component is examined in the order of occurrence. If and when the condition expression is satisfied the content in that component is rendered. Then all further contrib:When components are ignored. The optional contrib:Otherwise component is also automatically ignored. If none of the conditions in any contrib:When component is satisfied , then the contrib:Otherwise component is automatically selected (if it is present) and the content associated with that element is rendered.

The body of the contrib:Choose component can only contain:
  • White spaces
    May appear anywhere around the When and Otherwise components.
  • 1 or more When components
    Must all appear before Otherwise.
  • 0 or 1 Otherwise component
    Must be the last component nested within Choose

For simple conditional testing, use the Conditional component.

See Also
Conditional, contrib:When, contrib:Otherwise
Parameters
Name Type Direction Required Default Description
condition boolean in no false The condition to be met. If this value is true, then the wrapped elements will be rendered.
element String in no   If specified, then the component acts like an Any, emitting an open and close tag. Informal parameters are applied to the tag. If no element is specified, informal parameters are ignored.

Body: rendered
Informal parameters: allowed
Reserved parameters: invert

Examples

The following sample code shows how the text rendered depends on a user’s membership category.


              <span jwcid="@contrib:Choose>
                 <span jwcid="@contrib:When" condition='ognl:"visitor".equals(user.category)'>
                    ...
                 </span>
                 <span jwcid="@contrib:When" condition='ognl:"member".equals(user.category)'>
                    ...
                 </span>
                 <span jwcid="@contrib:When" condition='ognl:"customer".equals(user.category)'>
                    <span jwcid="@contrib:Choose>
                       <span jwcid="@contrib:When" condition='ognl:"person".equals(user.profile)'>
                          ...
                       </span>
                       <span jwcid="@contrib:When" condition='ognl:"enterprise".equals(user.profile)'>
                          ...
                       </span>
                    </span>
                 </span>
                 <span jwcid="@contrib:Otherwise">
                    ...
                 </span>
              </span>
            
An if/then/else statement can be easily achieved as follows:
			
              <span jwcid="@contrib:Choose">
                 <span jwcid="@contrib:When" condition="ognl: count == 0">
                    Your search did not match any documents.
                 </span>
                 <span jwcid="@contrib:Otherwise">
                    <span jwcid="@Insert" value="ognl: count">51</span>&nbsp;documents matched your selection.
                 </span>
              </span>
			  

Component Index contrib:FormConditional