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

4

20

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

Brutus answered 24/10, 2012 at 19:57 Comment(0)
A
43

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>
Adelbert answered 24/10, 2012 at 20:13 Comment(4)
Note that the reusable block is run every time. This may make it impractical for including data that must be calculated. But given how messy defining can be, you could consider creating a reusable block that generates the data on the first run and uses the cached copy in the future. However, this complicates the reusable blocks. Hopefully a future version of Play will make it cleaner to define variables.Dayna
Does this really mean the template compiler will not allow simply placing scala code within the html style attribute?Papaveraceous
How do you use this block?Oleg
Can you then call color repeatedly in the template?Oleg
A
10

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>
}
Ardussi answered 4/2, 2016 at 8:29 Comment(2)
Don't you need to refer to the defined variable as @customers?Oleg
It is already reffered as customers. Inside the @for() statement. (You should use only one @ sign per whole statement.)Ardussi
C
9

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>
Conchoidal answered 26/2, 2014 at 15:28 Comment(4)
very nice! Inside the template you can change the variable @{i=5}Tiertza
How would this actually work?Papaveraceous
@matt check this #12031646Conchoidal
how does it address the conditional aspect of this question though?!Papaveraceous
L
2

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

Leeann answered 10/12, 2016 at 20:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.