playframework - check if image exists with <image src="@routes.Assets.at("public/images", "logo.png")"> -
in playframework load images using @routes.assets.at
like
but want load image if logo.png available. because in case there no image showing empty image space.
is there syntax
@routes.assets.at("public/images", "logo.png").getorelse() kind of .. return type not of type option here.
i doubt approach correct 1 since images need width, height , alt attribute. if have data should know image exists.
you can create template 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"/> }
and use way:
<hr> @img("images/favicon.png", 16, 16, "play framework logo") <hr> @img("images/not-existing.png", 16, 16, "foo bar") <hr>
so result to:
<hr> <img src="/assets/images/favicon.png" width="16" height="16" alt="play framework logo"/> <hr> <hr>
project: https://github.com/schleichardt/stackoverflow-answers/tree/so18605473
Comments
Post a Comment