玩!框架:在模板中定义一个变量? [英] Play! framework: define a variable in template?
本文介绍了玩!框架:在模板中定义一个变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在向模板传递一个 Event
对象,我需要做的是检查 @event.getSeverity
值.如果值为正,我想将特定的
着色为绿色.如果值为负,我想将特定的
着色为红色.我找不到定义变量的方法.是否可以?应该是我觉得.
无论如何,最简单的方法是什么?
谢谢
解决方案 如 Play 文档 您可以使用 @defining
助手.
@defining(if (event.getSeverity > 0) "green" else "red") { color =><div style="background-color: @color">foo</div>}
或者你可以使用可重用块
@severityColor(event: Event) = @{如果(event.getSeverity > 0)绿色"否则红色"}<div style="background-color: @severityColor(event)">foo</div>
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>
这篇关于玩!框架:在模板中定义一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文