Reputation: 878
I am using Play Framework 2.5, dealing with scala.html and Twirl templates. Can someone please help me understand how to set the "key" on my span to be my "val id"?
I cannot seem to get this to work within html element attributes.I am not sure what syntax to use get my val id written to my span key.
Thanks for any help.
@{
val rank = player.rank
val id = player.id
if(rank == "Great") {
<span key="{id}">{rank}</span>
} else if(rank == "Good") {
<span key="{id}">{rank}</span>
}
}
Upvotes: 1
Views: 2011
Reputation: 878
I would up using an @defining(){} to solve my problem. Twirl does not have else if. I will just default to if statements. This is how I solved it.
@defining(player.rank) { rank =>
@defining(player.id) { id =>
@if(rank == "Great") {
<span key="@id">@rank</span>
}
@if(rank == "Good") {
<span key="@id">@rank</span>
}
<select key="@id">
@for((key, value) <- model.getAllRankings()){
<option id="@key" @if(rank == value) {selected}>@value</option>
}
</select>
}
}
Upvotes: 2
Reputation: 7591
You can't add html inside a @{ }
block. What you want is probably something like this:
@if(player.rank == "Great") {
<span key="@player.id">@player.rank</span>
} else if(player.rank == "Good") {
<span key="@player.id">@player.rank</span>
}
Declaring variables in these templates is not as easy as it seems. Check the documentation for some examples.
Upvotes: 2