Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Scrollbar

 

...

Excerpt
hiddentrue

Performing the equivalent of a "switch" statement in your component template using blocks and a delegate

...

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.

 

Div
stylewidth: 20em; float:right
Panel
borderColor#eee
titleBGColor#eee
titleJumpStart Demo

 If, Not, Negate, Switch, Else, Unless

Wiki Markup
{float:right|background=#eee|padding=0 1em} *JumpStart Demo:* [If, Not, Negate, Switch, Else, Unless|http://jumpstart.doublenegative.com.au/jumpstart/examples/lang/ifnotnegateswitchelseunless] {float}

In cases where you have to distinguish multiple cases, the 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.

...

Code Block
languagexml
titleSwitchMe.tml
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_04.xsd">
    <body>
        <h1>Switch</h1>

        <t:delegate to="case"/>

        <t:block t:id="case1">
            Here is the content for case1.
        </t:block>

        <t:block t:id="case2">
            Here is the content for case2.
        </t:block>
        
        <t:block t:id="case3">
            Here is the content for case3.
        </t:block>
        
        <t:block t:id="case4">
            Here is the content for case4.
        </t:block>
    </body>
</html>

...