I'm passing to a template an Event
object and what I need to do is checking @event.getSeverity
value. if the value is positive, I want to color a specific <div>
in green. if the value is negative I want to color a specific <div>
in red.
I couldn't find a way to define a variable. is it possible? it should be I think.
anyhow, what's the simplest way accomplishing this?
thanks
As stated in the Play documentation you can use the @defining
helper.
@defining(if (event.getSeverity > 0) "green" else "red") { color =>
<div style="background-color: @color">foo</div>
}
Or you can use a reusable block
@severityColor(event: Event) = @{
if (event.getSeverity > 0) "green" else "red"
}
<div style="background-color: @severityColor(event)">foo</div>
Another variant. Works fine if declared after import section. Otherwise may cause some errors ("value not found")
@import play.i18n.Messages
@import models.Customers
@customers = @{Customers.allAccepted()}
...
@if(customers.size()>0) {
<ul>
@for(customer <- customers) {
<li>
<a href="/filters/customer/@customer.id">@customer.name</a>
</li>
}
</ul>
}
try this in scala template
@import java.math.BigInteger; var i=1; var k=1
and for string
@import java.lang.String; val name="template"
in question aspect
@import java.lang.String; var color="red"
@if(event.getSeverity>0){
@{color="green"}
}
<div style="background-color: @color">foo</div>
"for" comprehensions can be also useful some times:@for(id <- products.keys;
product = products(id);
author = product.author.getOrElse("N/A")) {...
@product.name
...
@author
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With