2

Using Facelets and writing some XHTML, I cannot figure out how to create an element and then add attributes later, like in xslt if you wanted to conditionally add an attribute:

<xsl:element name="div">
    <xsl:attribute name="style">color:blue;</xsl:attribute>
</xsl:element>

Google gave some examples with JSP taglib similar to

<jsp:element name="div">
     <jsp:attribute name=".">...</jsp:attribute>
</jsp:element>

That library is not provided as standard in Facelets and searching the docs of the ones that are included doesn't show anything obvious.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MitchBroadhead
  • 811
  • 14
  • 23

1 Answers1

9

Use <c:if><f:attribute> on a real JSF component.

<h:panelGroup layout="block">
    <c:if test="#{bean.condition}"><f:attribute name="style" value="color:blue;"/></c:if>
</h:panelGroup>

By the way, you should really be using fullworthy CSS classes in CSS stylesheet files instead of tight-coupling style attributes over all place in markup.

You could conditionally declare style classes as below:

<h:panelGroup layout="block" styleClass="#{bean.condition ? 'foo' : 'bar'}" />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • isn't the use of the **c:** tags now deprecated? – AlanObject Jan 07 '16 at 16:28
  • @AlanObject: http://stackoverflow.com/questions/3342984/jstl-in-jsf2-facelets-makes-sense – BalusC Jan 07 '16 at 16:29
  • 1
    Is there also a solution, if the `` would be inside a `` and the condition is depending on the iteration-variable? You can't use `` in this case due to problems with build/render view time. – Martin Höller Feb 14 '19 at 10:44