reen
reen

Reputation: 2502

Can this Play template code be simplified (avoid if/else tag)?

I have the following html code:

#{if title == 'Subnet' }
    <li><a href="@{SubnetController.list}" class="selected">&{'subnet'}</a></li>
#{/if}
#{else}
    <li><a href="@{SubnetController.list}">&{'subnet'}</a></li>
#{/else}

Is it possible to do that with less code, maybe using a groovy operator I am not aware of?

Upvotes: 2

Views: 1142

Answers (4)

hmehdi
hmehdi

Reputation: 101

following should also do the trick:

 <li> <a href="@{SubnetController.list}" ${title == 'Subnet'? 'class="selected"'.raw() : ''}> 
 ${'subnet'}</a></li>

Upvotes: 0

ontk
ontk

Reputation: 942

I usually use custom tags to encapsulate presentation logic in my templates so in your case, I'd have:

<li><appName:subnetLink title=${title} /></li>

My 2 cents.

Upvotes: 0

Lexi
Lexi

Reputation: 11

following should also do the trick:

<li><a href="@{SubnetController.list}" #{title == 'Subnet'? 'class="selected"' : ''}>&{'subnet'}</a></li>

Upvotes: 1

sojin
sojin

Reputation: 4694

<li><a href="@{SubnetController.list}" #{if title == 'Subnet'} class="selected" #{/if} >&{'subnet'}</a></li>

should do the trick.

Upvotes: 6

Related Questions