Nacho321
Nacho321

Reputation: 1991

Conditionally displaying a block of HTML based on presence of resource bundle key

I'm using JSF, and I have to load a bundle called Extra.

<f:loadBundle basename="com.ni.lib.extra.delivery.ExtraBundle" var="extra" />

Inside that extra variable, there's a value called downtime_notice. Now, if that value is NOT empty, I have to show a css segment with the text contained within the downtime_notice value. Something like this (of course this doesn't work):

if(extra.donwtime_notice!=''){
            <div class="pnx-msg pnx-msg-warning clearfix">
                <i class="pnx-msg-icon pnx-icon-msg-warning"/>
                <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
            </div>
            </br>
}

I can use javascript, just in case. Any ideas? Thanks!

Upvotes: 1

Views: 203

Answers (1)

BalusC
BalusC

Reputation: 1109322

You can use <ui:fragment> or <h:panelGroup> to conditionally render content. You can use the empty keyword in EL to check if a variable is not null or empty. So, all with all this should do:

<ui:fragment rendered="#{not empty extra.donwtime_notice}">
    <div class="pnx-msg pnx-msg-warning clearfix">
        <i class="pnx-msg-icon pnx-icon-msg-warning"/>
        <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
    </div>
</ui:fragment>

Or, using <h:panelGroup layout="block"> which renders a <div> already:

<h:panelGroup layout="block" styleClass="pnx-msg pnx-msg-warning clearfix" rendered="#{not empty extra.donwtime_notice}">
    <i class="pnx-msg-icon pnx-icon-msg-warning"/>
    <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
</h:panelGroup>

Note that some may opt to use <f:verbatim> for the job, but this tag is deprecated since JSF2.

See also:


Unrelated to the concrete problem, the </br> tag is invalid HTML, so I omitted it form the examples.

Upvotes: 2

Related Questions