If, Not, Negate, Switch, Else, Unless

Tapestry has several ways to condition blocks in a template. JumpStart uses the first three shown below.
Choose a value for myBoolean:           
1. If with !.
myBoolean == Boolean.TRUE.
2. If with negate.
myBoolean == Boolean.TRUE.
3. Delegate. (Allows "Switching Cases" logic, provided in the page class.)
myBoolean == Boolean.TRUE.
4. If with else. (Pros: tests the condition once. Cons: the else is nested, hard to see.)
myBoolean == Boolean.TRUE.
5. If with Unless.
myBoolean == Boolean.TRUE.
References: If, Delegate, t:block, Block, Switching Cases, Unless, Radio, Radio Group, Form.

Home

IfNotNegateSwitchElseUnless.tml


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- We need a doctype to allow us to use special characters like &nbsp; 
     We use a "strict" DTD to make IE follow the alignment rules. -->
     
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd" xmlns:p="tapestry:parameter">
<head>
    <link rel="stylesheet" type="text/css" href="${context:css/examples/examples.css}"/>
</head>
<body>
    <h1>If, Not, Negate, Switch, Else, Unless</h1>

    Tapestry has several ways to condition blocks in a template. JumpStart uses the first three shown below.

    <div class="eg">
        <form t:type="form" t:id="myForm">
        
            Choose a value for myBoolean:&nbsp;&nbsp;
            
            <!-- We use a String, not a Boolean, in the radio group value so that we can represent null. If a Boolean is set to null 
                then Tapestry coerces it to FALSE. See https://issues.apache.org/jira/browse/TAPESTRY-1928 . -->
                
            <t:RadioGroup t:id="valueForMyBoolean">
                <t:Radio t:id="radioT" value="literal:T" label="Boolean.TRUE" onclick="this.form.submit()"/>
                <t:label for="radioT"/>&nbsp;&nbsp;
                <t:Radio t:id="radioF" value="literal:F" label="Boolean.FALSE" onclick="this.form.submit()"/>
                <t:label for="radioF"/>&nbsp;&nbsp;
                <t:Radio t:id="radioN" value="literal:N" label="null" onclick="this.form.submit()"/>
                <t:label for="radioN"/>&nbsp;&nbsp;
            </t:RadioGroup>
            
        </form>
    </div>
    
    1. <code>If</code> with <code>!</code>.

    <div class="eg">
        <t:if test="myBoolean">
            <t:delegate to="block:t"/>
        </t:if>
        <t:if test="!myBoolean">
            <t:delegate to="block:notT"/>
        </t:if>
    </div>
    
    2. <code>If</code> with <code>negate</code>.

    <div class="eg">
        <t:if test="myBoolean">
            <t:delegate to="block:t"/>
        </t:if>
        <t:if test="myBoolean" negate="true">
            <t:delegate to="block:notT"/>
        </t:if>
    </div>
    
    3. <code>Delegate</code>. (Allows "Switching Cases" logic, provided in the page class.)

    <div class="eg">
        <t:delegate to="case"/>
    </div>

    4. <code>If</code> with <code>else</code>. (Pros: tests the condition once. Cons: the else is nested, hard to see.)

    <div class="eg">
        <t:if test="myBoolean">
            <t:delegate to="block:t"/>
            <p:else>
                <t:delegate to="block:notT"/>
            </p:else>
        </t:if>
    </div>
    
    5. <code>If</code> with <code>Unless</code>.

    <div class="eg">
        <t:if test="myBoolean">
            <t:delegate to="block:t"/>
        </t:if>
        <t:unless test="myBoolean">
            <t:delegate to="block:notT"/>
        </t:unless>
    </div>

    <t:block id="t">
        myBoolean == Boolean.TRUE.
    </t:block>
    <t:block id="notT">
        myBoolean != Boolean.TRUE.
    </t:block>
    <t:block id="f">
        myBoolean == Boolean.FALSE.
    </t:block>
    <t:block id="n">
        myBoolean == null.
    </t:block>

    References: 
    <a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/If.html">If</a>, 
    <a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/Delegate.html">Delegate</a>, 
    <a href="http://tapestry.apache.org/component-templates.html#ComponentTemplates-The%3Ct%3Ablock%3EElement">t:block</a>, 
    <a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/Block.html">Block</a>, 
    <a href="http://tapestry.apache.org/switching-cases.html">Switching Cases</a>, 
    <a href="http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/Unless.html">Unless</a>, 
    <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/Radio.html">Radio</a>, 
    <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/RadioGroup.html">Radio Group</a>, 
    <a href="http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/corelib/components/Form.html">Form</a>.<br/><br/>

    <t:pagelink page="Index">Home</t:pagelink><br/><br/>
    
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/lang/IfNotNegateSwitchElseUnless.tml"/>
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/pages/examples/lang/IfNotNegateSwitchElseUnless.java"/>
    <t:sourcecodedisplay src="/web/src/main/java/jumpstart/web/css/examples/examples.css"/>
</body>
</html>

IfNotNegateSwitchElseUnless.java


package jumpstart.web.pages.examples.lang;

import org.apache.tapestry5.Block;
import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

public class IfNotNegateSwitchElseUnless {

    // Screen fields

    @Property
    @Persist(PersistenceConstants.FLASH)
    // We use a String, not a Boolean, in the radio group value so that we can represent null. Boolean can't represent
    // null because Tapestry will coerce it to Boolean.FALSE. See https://issues.apache.org/jira/browse/TAPESTRY-1928 .
    private String valueForMyBoolean;

    @Property
    private Boolean myBoolean;

    // Generally useful bits and pieces

    @Inject
    Block t, f, n;

    // The code

    void setupRender() {

        // First time in, valueForMyBoolean will be null.

        if (valueForMyBoolean == null) {
            valueForMyBoolean = "T";
        }

        // Set myBoolean based on valueForMyBoolean.

        if (valueForMyBoolean.equals("T")) {
            myBoolean = Boolean.TRUE;
        }
        else if (valueForMyBoolean.equals("F")) {
            myBoolean = Boolean.FALSE;
        }
        else if (valueForMyBoolean.equals("N")) {
            myBoolean = null;
        }
        else {
            throw new IllegalStateException(valueForMyBoolean);
        }
    }

    public Block getCase() {

        // If myBoolean was an int or enum we could use switch/case logic instead of if/else -
        // see http://tapestry.apache.org/switching-cases.html

        if (myBoolean == null) {
            return n;
        }
        else if (myBoolean == Boolean.TRUE) {
            return t;
        }
        else {
            return f;
        }
    }

}

examples.css


body            { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 13px; font-weight: normal; color: #333; line-height: 17px; }
h1              { font-size: 26px; line-height: 20px; } /* For IE 7 */
form            { margin: 0; }                  

.eg             { margin: 20px 0; padding: 20px; color: #888; 
                    border: 1px solid #ddd; border-radius: 4px; -webkit-border-radius: 4px; -mox-border-radius: 4px; }

a               { text-decoration: none; color: #3D69B6; }
a:hover         { text-decoration: underline; }

/* For BeanDisplay */
.eg dl          { margin: 0; color: #333; }
.eg dl.t-beandisplay dd.id  { display: inline; margin-left: 0px; }  /* IE 7 hack */