<!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
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:
<!-- 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"/>
<t:radio t:id="radioF" value="literal:F" label="Boolean.FALSE" onclick="this.form.submit()"/>
<t:label for="radioF"/>
<t:radio t:id="radioN" value="literal:N" label="null" onclick="this.form.submit()"/>
<t:label for="radioN"/>
</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>