Search code examples
javascalaplayframeworkplayframework-2.0template-engine

Play! framework: define a variable in template?


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


Solution

  • 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>