Dealing with Conditional Content

Many web frameworks allow you to specify page content that appears conditionally from within the html or template file with a tag or logical instruction of some sort, such as an if tag. In keeping with the seperation of view from the controller, wicket does not offer this functionality.

A common question, then, is how to conditionally render some content. Perhaps you want to display a form element only if appropriate, or a footer only on pages concerning a particular subject.

In wicket, there are two ways of dealing with this situation:

The Visibility Method

Example:

 JSP: 
   <c:if condition="blah">
     <input type="text" name="comp" value="${val}"/>
   </c:if>
 Wicket:
   code:
   add(new TextField("comp",...) {
       boolean isVisible() {
           return blah;
        }
    }
 
   template:
   <input type="text" wicket:id="comp"/>

Extended version:

 JSP:
   <c:if condition="blah">
     Answer me: <input type="text" name="comp" value="${val}"/>
   </c:if>
 Wicket:
   code:
   
   WebMarkupContainer blahRow = new WebMarkupContainer ("blahrow") {
     public boolean isVisible() {
       return blah;
     }
   }
   blahRow.add(new TextField("comp",...));
 
   template:
   &lt;span wicket:id="blahrow">
     Answer me: <input type="text" wicket:id="comp"/>
   &lt;/span>

The Panel Method