1

I'd like to conditionally render attributes of a plain HTML <div> element. I tried as below using <c:if>:

<ui:composition
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:c="http://java.sun.com/jstl/core"
        xmlns:f="http://java.sun.com/jsf/core" 
        xmlns:h="http://java.sun.com/jsf/html">        

    <div data-product-id="#{ID}" 
        <c:if test="${not empty testvalue1}">
            data-test1-for="#{testvalue1}"
        </c:if>
        <c:if test="${not empty testvalue2}">
            data-test2-for="#{testvalue1}"
        </c:if>
    >
        div content
    </div>
</ui:composition>

However, it produced a Facelet exception:

com.sun.facelets.FaceletException: Error Parsing /assets/template/module/container/product/product-marker.xhtml: Error Traced[line: 9] Element type "div" must be followed by either attribute specifications, ">" or "/>"

How can I conditionally render attributes of a plain HTML <div> element?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Patan
  • 17,073
  • 36
  • 124
  • 198
  • I'm not familiar enough with JSF, but I do know that you can't use that syntax. There's no such thing as nested tags. Now [here](http://stackoverflow.com/questions/12897202/conditionally-render-elements-attribute-in-a-composite-component) is a question that explains how to do it with composite components; no idea if it works with plain HTML elements too. – Mr Lister Mar 23 '15 at 10:51

1 Answers1

1

You don't need those if statements. If the values are empty, attributes won't be rendered on the page automatically.

Crepi
  • 645
  • 5
  • 15
  • That is just plain wrong, the div will have empty attributes "data-test1-for" and "data-test2-for". In case of boolean attributes thats a problem! – DaniEll Dec 06 '19 at 11:22