Wicket: Conditional display in Template

theomega picture theomega · Aug 7, 2009 · Viewed 8.3k times · Source

Hy, I want to display a certain part (a div for example) of my wicket-template only under a certain condition (for example only if I have the data to fill it). The problem is:

If I only add the panel (filling the div) if I got the data, an exception is thrown every time I call the page without the data (because the referenced wicket-id is not added to the component-tree).

The only solution which came to my mind was to add a empty panel if there is no data. This is not an ideal solution because I got some unneeded code in the java-code and many empty divs in my rendered html.

So is there a better solution to include several parts of a wicket-template only under a condition?

Answer

Karussell picture Karussell · Aug 1, 2010

Although this is an old question here could be one more solution: wicket:enclosure (and this )

Update: Now I needed this functionality by my self (for jetwick). I'm using WebMarkupContainer one for loggedIn state and one for loggedOut state and set the right visibility:

if (loggedIn()) {            
   WebMarkupContainer loggedInContainer = new WebMarkupContainer("loggedIn");
   //## do something with the user
   User user = getUserSomeWhere();
   loggedInContainer.add(new UserSearchLink("userSearchLink"));
   add(loggedInContainer);
   add(WebMarkupContainer("loggedOut").setVisible(false));
} else {
   add(new WebMarkupContainer("loggedIn").setVisible(false));
   WebMarkupContainer loggedOutContainer = WebMarkupContainer("loggedOut");
   loggedOutContainer.add(new LoginLink() {...});
   add(loggedOutContainer);
}

The advantage of this for me is that I prevent a NullpointerExc in the //## marked line and the enclose feature of wicket would look more ugly to me in this case I think.