Playframework 使用<;检查图像是否存在;图像src="@routes.Assets.at(“public/images”和“logo.png”)网站&燃气轮机;

Playframework 使用<;检查图像是否存在;图像src="@routes.Assets.at(“public/images”和“logo.png”)网站&燃气轮机;,playframework,playframework-2.0,Playframework,Playframework 2.0,在playframework中加载图像时,我使用@routes.Assets.at 像 但我只想在logo.png可用时加载此图像。 因为在没有图像的情况下,它显示的是空的图像空间 有类似的语法吗 @routes.Assets.at(“public/images”,“logo.png”).getorelse()类。。但是返回类型不是这里的可选类型。我怀疑您的方法是否正确,因为图像需要宽度、高度和alt属性。如果您有这些数据,您应该已经知道图像存在 您可以创建一个模板img.scala.html:

在playframework中加载图像时,我使用@routes.Assets.at

但我只想在logo.png可用时加载此图像。 因为在没有图像的情况下,它显示的是空的图像空间

有类似的语法吗


@routes.Assets.at(“public/images”,“logo.png”).getorelse()类。。但是返回类型不是这里的可选类型。

我怀疑您的方法是否正确,因为图像需要宽度、高度和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"/>
}
@(路径:字符串,宽度:Int,高度:Int,alt:String=”“)
@导入play.api.play.resource
@导入play.api.play.current
@if(资源(“public/”+path).isDefined){
}
并以这种方式使用它:

<hr>
@img("images/favicon.png", 16, 16, "play framework logo")
<hr>
@img("images/not-existing.png", 16, 16, "foo bar")
<hr>

@img(“images/favicon.png”,16,16,“play framework徽标”)
@img(“images/notexisting.png”,16,16,“foobar”)
因此,这将导致:

<hr>
<img src="/assets/images/favicon.png" width="16" height="16" alt="play framework logo"/>
<hr>
<hr>




项目:

所以你不知道编译后图像是否会在那里?原因如果是这种情况,您需要了解Play如何在生产模式和开发模式下管理资产文件。在Prod模式下,在Prod模式下启动服务器后添加到资产中的任何文件都将无法识别。我正在使用。if(resource(“public/”+path).isDefined)是我要找的。谢谢你明确的答复!。