Search code examples
htmljsfjsf-2conditional-comments

How would I mimic this conditional inclusion of resources in JSF 2?


In short, I'd like to do this (the conditional part, i know how to include with <h:outputStyleSheet>):

<link rel="stylesheet" href="[path-to-dist]/leaflet.css" />
<!--[if lte IE 8]>
    <link rel="stylesheet" href="[path-to-dist]/leaflet.ie.css" />
<![endif]-->

In JSF 2.

Thanks!


Solution

  • Use <h:outputText escape="false">

    <h:outputText value="&lt;!--[if lte IE 8]&gt;&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;[path-to-dist]/leaflet.ie.css&quot;/&gt;&lt;![endif]--&gt;" escape="false" />
    

    Yes, it's a line of ugliness, but there's really no other way as Facelets is inherently a XML based view technology.


    Update: if you happen to use the JSF utility library OmniFaces, you can just use its <o:conditionalComment> component.

    <o:conditionalComment if="lte IE 8">
        <link rel="stylesheet" href="[path-to-dist]/leaflet.ie.css" />
    </o:conditionalComment>