Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a class to html element conditionally with grails tags

Tags:

grails

gsp

With tags, you can do this in a gsp:

<g:if test="${someBean?.aCondition}">
  <div class="aSection">
  ...
  </div>
</g:if>

What I really want to do is add a second 'class' that either contains the 'display:none' or 'display:block' attributes based the value of '${someBean?.aCondition}'.

The final html would like this:

<div class="aSection hiddenItem">
...
</div>

(the div would have 'shownItem' for its class if ${someBean?.aCondition} is true)

The corresponding css:

.shownItem
{
  display: block;
}
.hiddenItem
{
  display: none;
}

What's a good way to achieve this?

like image 392
Robin Jamieson Avatar asked Nov 20 '09 19:11

Robin Jamieson


2 Answers

Easy enough:

<div class="aSection ${someBean?.aCondition ? 'shownItem':'hiddenItem'}">
...
</div>

You can use ${} blocks inside html attributes, no problem, just be sure not to use any double-quotes in your expression block, as that confuses things.

like image 150
billjamesdev Avatar answered Nov 18 '22 10:11

billjamesdev


Or if you don't want the attribute displayed at all on some (say for an autofocus tag) condition:

<input name="email" type="text" class="input-small" placeholder="Email" ${!flash.email ? 'autofocus="autofocus"':''} value="${flash.email}">
like image 39
sparkyspider Avatar answered Nov 18 '22 10:11

sparkyspider