玩!框架:在模板中定义变量? [英] Play! framework: define a variable in template?

查看:79
本文介绍了玩!框架:在模板中定义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向模板传递 Event 对象,我需要做的是检查 @ event.getSeverity 价值。如果值为正,我想以绿色为特定的< div> 着色。如果值为负,我想用红色为特定的< 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?

谢谢

推荐答案

播放文档,您可以使用 @defining 帮助。

@defining(if (event.getSeverity > 0) "green" else "red") { color =>
    <div style="background-color: @color">foo</div>
}

或者您可以使用可重复使用的块

@severityColor(event: Event) = @{
    if (event.getSeverity > 0) "green" else "red"
}

<div style="background-color: @severityColor(event)">foo</div>

这篇关于玩!框架:在模板中定义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆