Play! framework: define a variable in template?

socksocket picture socksocket · Oct 24, 2012 · Viewed 23.2k times · Source

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

Answer

Alex picture Alex · Oct 24, 2012

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>