Thursday, April 2, 2009

Tapestry 5 if else condition

You may at some point need to have simple conditions in your tapestry view. Tapestry 5.1 use this version
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd" xmlns:p="tapestry:parameter">
<t:if test="loggedIn">
  Hello, ${userName}!
  <p:else>
    Click <a t:type="actionlink" t:id="login">here</a> to log in.
  </p:else>
</t:if>
</html>
Older versions of Tapestry 5 use this version
<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
<t:if test="loggedIn">
  Hello, ${userName}!
  <t:parameter name="else">
    Click <a t:type="actionlink" t:id="login">here</a> to log in.
  </t:parameter>
</t:if>
</html>

3 comments:

  1. hahaha I was looking for the code to add an "else" in a tapestry 5 "if" clause and your page was the first hit in google.

    YOU'RE FAMOUS!!!

    ReplyDelete
  2. Hi

    Thanks for the tips. But I get a question. Why the prefix in else condition uses p instead of t, which is normally used by other tag e.g. t:if?

    Thanks again for the post,

    This tips saves my time.

    ReplyDelete
  3. Hi,
    I need to customize a file upload field (sometimes I want it to be required, sometimes not)

    In order to do so, I have tried to use the conditionnal syntax you mentionned :

    ≤i≥
    ≤t:if test="fileExists"≥
    ≤input t:type="upload" t:id="file" /≥
    ≤p:else≥
    ≤input t:type="upload" t:id="file" validation="required" /≥
    ≤/p:else≥
    ≤/t:if≥
    ≤/i≥



    Unfortunnately, this syntax generates the following error :

    already contains a child component with id 'file'. Embedded component ids must be unique (excluding case, which is ignored).

    It sounds like a tapestry's bug (regarding syntax checking) : in fact there is no duplicated id, as there is an exclusion between those 2 blocks.

    Do u have any idea of a workaround?

    Thanks
    Regards
    Emmanuel

    ReplyDelete