user2400597
user2400597

Reputation:

How to show images using Play framework and Scala in the view page

Using the Play framework, I'm trying to show the image friend_request.png in the view page.

This is my code but the image is not showing:

<img src=@("images/friend_request.png") width="28" height="22" />

Upvotes: 8

Views: 10906

Answers (2)

Matthias Braun
Matthias Braun

Reputation: 34373

Skyr's answer doesnt' work for my version of Play (2.4.2). Using it yields the following compilation error:

value at is not a member of controllers.ReverseAssets

After consulting the documentation, this works for me:

<img src="@routes.Assets.versioned("images/yourImage.png")">

where yourImage.png is in public/images.


If you want to speed up page loading via caching and compressing your images (and all the other assets of your web app), try this:

Make sure that your plugins.sbt contains these entries:

addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-gzip" % "1.0.0")

Then, in your build.sbt, add this:

// Apply RequireJS optimizations, create a checksum, and zip each asset
pipelineStages := Seq(rjs, digest, gzip)

Upvotes: 4

Skyr
Skyr

Reputation: 1010

I assume you want to include a static image, and since you're talking about Scala, I assume you're using Play Framework 2.

The appropriate place of the image in the standard project layout should be public/images/friend_request.png. You can then refer to the image:

<img src="@routes.Assets.at("images/friend_request.png")" width="28" height="22" />

For more information on assets (caching time, etc.), please refer to the Play Framework documentation.

Upvotes: 11

Related Questions