-6ren"> -在 playframework 加载图像我使用@routes.Assets.at 喜欢 但是我只想在这个 logo.png 可用时加载这个图像。因为在没有图像的情况下,它会显示空白图像空间。 有没有像-6ren">
gpt4 book ai didi

playframework - 检查图像是否存在

转载 作者:行者123 更新时间:2023-12-01 11:42:20 24 4
gpt4 key购买 nike

在 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"/>
}

并以这种方式使用它:

<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

关于playframework - 检查图像是否存在 <image src ="@routes.Assets.at("public/images", "logo.png")">,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18605473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com