JSF: conditionally render a list item (<li>)

George Armhold picture George Armhold · Sep 19, 2011 · Viewed 19k times · Source

I just inherited a project implemented in JSF. I have the following code which looks fine in Chrome, but Firefox renders the borders on the "empty" list items:

<ul>
    <li><a href="/home">Home</li>
    <li>
        <s:link view="/signup.xhtml" rendered="#{someCondition}">Sign Up</s:link>
    </li>
    <!-- etc... -->
</ul>

Which ends up looking like:

enter image description here

Is there a JSF tag to conditionally render the <li> ?

Answer

mrembisz picture mrembisz · Sep 19, 2011

If you do it as in @CoolBeans example, you will get a <span> around your <li>. In some cases it might disrupt your layout, besides you don't really want an extra tag under <ul>. To get rid of it, use <ui:fragment rendered="#{condition}" /> around your item instead of <h:panelGroup>.

Also you can use style attribute to hide an item:

<li style="display: #{condition ? 'list-item' : 'none'};" />