Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play! framework: define a variable in template? [duplicate]

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

like image 620
socksocket Avatar asked Oct 24 '12 19:10

socksocket


4 Answers

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>
like image 121
Alex Avatar answered Nov 05 '22 05:11

Alex


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>
}
like image 42
Rib47 Avatar answered Nov 05 '22 06:11

Rib47


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>
like image 9
Govind Singh Avatar answered Nov 05 '22 06:11

Govind Singh


"for" comprehensions can be also useful some times:
@for(id <- products.keys; product = products(id); author = product.author.getOrElse("N/A")) {... @product.name ... @author

like image 2
kairius Avatar answered Nov 05 '22 06:11

kairius