Kevingo Tsai
Kevingo Tsai

Reputation: 639

How to render different html elements in the play view template by a boolean value?

If I pass a boolean value to the view, for example "checked", how to show the different html elements according to the checked value(that is , true or false) ?

#{if ${checked}}
     <p>aaa</p>
#{/if}
#{else}
     <p>bbb</p>
#{/else}

I try the above code but failed...

Upvotes: 1

Views: 543

Answers (2)

kheraud
kheraud

Reputation: 5288

In addition to @aaberg response with an inline if :

<input type="checkbox" ${checked ? 'checked="checked"' : ''} />
<span>The previous checkbox is ${checked ? "checked" : "not checked"} !</span>

Upvotes: 3

aaberg
aaberg

Reputation: 2273

#{if checked}
     <p>aaa</p>
#{/if}
#{else}
     <p>bbb</p>
#{/else}

The ${} syntax, is only for printing values into your template. Inside the #{if } tag, playframework expects groovy syntax.

Also, check out the documentation for the #{if} tag.

Upvotes: 4

Related Questions