Sreejith G
Sreejith G

Reputation: 211

Conditional linking to an CSS file

In my JSF2 project I want to include following two style sheet to my Facelet page based on a condition which is to be obtained from a managed bean.

<link rel="stylesheet" href="css/menu_style_1.css" type="text/css" />
<link rel="stylesheet" href="css/menu_style_2.css" type="text/css" />

How can I achieve this?

Upvotes: 0

Views: 1902

Answers (1)

Daniel
Daniel

Reputation: 37061

try

<h:outputStylesheet  name="css/menu_style_1.css" target="head" rendered="#{myBean.myValue eq 'myCondition1'}"/>

and

<h:outputStylesheet name="css/menu_style_2.css" target="head" rendered="#{myBean.myValue eq 'myCondition2'}"/>

OR... you can do it in one line...

<h:outputStylesheet name="css/#{myBean.myValue == 'true' ? 'menu_style_1.css' : 'menu_style_2.css'}" target="head" />

I guess if you want to stick to the link tag you can try something like that

<link  rel="stylesheet" href="css/#{myBean.myValue == 'true' ? 'menu_style_1.css' : 'menu_style_2.css'}" type="text/css" />

Upvotes: 1

Related Questions