trueBLOCK
Performing the equivalent of a "switch" statement in your component template using blocks and a delegate
Switching Cases
With Tapestry's If
component you can only test one condition at a time. In order to distinguish multiple cases, you'd have to write complex nested if/else constructs in your page template and have a checker method for each test inside your page class.Delegate
component comes in. It delegates rendering to some other component, for example a Block
. For each case you have, you basically wrap the content inside a Block
that doesn't get rendered by default. You then place a Delegate component on your page and point it to a method inside your page class that will decide which of your Blocks should be rendered.
Imagine for example a use case, where you want to distinguish between 4 cases and you have an int property called whichCase
that should be tested against. Your page template would look as follows:
You can see, that the Delegate
component's to
parameter is bound to the case property of your page class. In your page class you therefore have a getCase()
method that is responsible for telling the Delegate
component which component should be rendered. For that we are injecting references to the Block}}s defined in your page template into the page class and return the according {{Block
in the getCase()
method.
Happy switching!