在播放框架中加载图像,我正在使用@routes.Assets.at
喜欢
但是,只有当这个logo.png可用时,我才想加载这个映像。因为在没有图像的情况下,它显示的是空图像空间。
有没有类似的语法
@routes.Assets.at(“公共/图像”,"logo.png").getorelse()有点..但是这里的返回类型不是类型选项。
发布于 2013-09-03 21:57:29
我怀疑您的方法是否正确,因为图像需要宽度、高度和alt属性。如果您有这些数据,您应该已经知道图像的存在。
您可以创建一个模板img.scala.html:
@(path: String, width: Int, height: Int, alt: String = "")
@import play.api.Play.resource
@import play.api.Play.current
@if(resource("public/" + path).isDefined) {
<img src="@routes.Assets.at(path)" width="@width" height="@height" alt="@alt"/>
}
并以这种方式使用:
<hr>
@img("images/favicon.png", 16, 16, "play framework logo")
<hr>
@img("images/not-existing.png", 16, 16, "foo bar")
<hr>
因此,它将导致:
<hr>
<img src="/assets/images/favicon.png" width="16" height="16" alt="play framework logo"/>
<hr>
<hr>
项目:https://github.com/schleichardt/stackoverflow-answers/tree/so18605473
https://stackoverflow.com/questions/18605473
复制