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: Conditional and Looping Components, If, Delegate, t:block, Block, Switching Cases, Unless, Radio, Radio Group, Form.

Home


<!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_4.xsd" xmlns:p="tapestry:parameter">
<body class="container">
    <h3>If, Not, Negate, Switch, Else, Unless</h3>

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

    <div class="eg">
        <t: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>
            
        </t: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/component-reference.html#ComponentReference-ConditionalandLoopingComponents">Conditional and Looping Components</a>, 
    <a href="http://tapestry.apache.org/5.4/apidocs/org/apache/tapestry5/corelib/components/If.html">If</a>, 
    <a href="http://tapestry.apache.org/5.4/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.4/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.4/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:tabgroup>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/lang/IfNotNegateSwitchElseUnless.tml"/>
        <t:sourcecodetab src="/web/src/main/java/jumpstart/web/pages/examples/lang/IfNotNegateSwitchElseUnless.java"/>
        <t:sourcecodetab src="/web/src/main/resources/META-INF/assets/css/examples/plain.css"/>
    </t:tabgroup>
</body>
</html>


package jumpstart.web.pages.examples.lang;

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

@Import(stylesheet = "css/examples/plain.css")
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;
        }
    }

}


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