htmljsfjstltrinidadconditional-rendering

How can I envelope something in HTML element conditionally


On a page created with use of JSF, JSTL and Trinidad, I have some tag called with many attributes.

<tr:selectBooleanRadio
        group="#{groupId}"
        id="#{groupId}_#{Id}_radio"
        selected="#{value.yesterday}"
        text="#{msg.ib_selectTimeIntervalRadio_yesterday_label}"
        shortDesc="#{ib:fn_coalesce(title,label)}"
        simple="#{ib:fn_coalesce(simple,true)}"
        styleClass="#{ib:fn_coalesce(styleClass,'selectTimeIntervalRadio')}"/>

I want to show it in <td> element or directly, conditionally. Now I do it as:

<c:when test="#{not horizontal}">
    <tr>
        <td>
            <tr....>
        </td>
    </tr>
</c:when>
<c:when test="#{horizontal}">
    <tr...>
</c:when>

This way I repeat the same piece of code twice, which is obviously bad. What can I do?

IMHO, it is useless to create a new custom component for the tr... call - calling of that will have all the same problems.

Maybe, I can envelope one tag in another conditionally? Can I?

The other way could be such: put the tr... tag in some sort of variable and use the same when construction with this variable. Maybe, it is possible?

Do you know another way how to escape this code duplication?


Solution

  • If you're using legacy JSP, wrap it in <f:verbatim>.

    <f:verbatim rendered="#{not horizontal}"><tr><td></f:verbatim>
        <tr:selectBooleanRadio ... />
    <f:verbatim rendered="#{not horizontal}"></td></tr></f:verbatim>
    

    If you're using JSPX or Facelets, which would produce a XML error on above construct which is illegal in XML, escape it in a <h:outputText escape="false">.

    <h:outputText value="&lt;tr&gt;&lt;td&gt;" escape="false" rendered="#{not horizontal}" />
        <tr:selectBooleanRadio ... />
    <h:outputText value="&lt;/td&gt;&lt;/tr&gt;" escape="false" rendered="#{not horizontal}" />