ripper234
ripper234

Reputation: 230346

Rendering inline HTML from Play Framework using Scala?

How do I render inline HTML from Play 2, without using an external template file?

def checkStatus = Action {
  val status = ...
  if (status.ok) {
    Ok("Status OK")
  } else {
    // Oops, this renders the literal text instead of the HTML I wanted:

    Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>")
  }
}

Upvotes: 1

Views: 451

Answers (2)

biesior
biesior

Reputation: 55798

When you are rendering the view, Play recognizes its type (at least for html, xml, and txt), but when you want to return common String you need to instruct which type it is (otherwise it's assumed as text/plain)

According to Manipaliting response doc you need to return with with the type:

 BadRequest("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as("text/html")

Upvotes: 2

Infinity
Infinity

Reputation: 3441

Ok("Hello World!") sets the Content-Type header to text/plain unless explicitly specified:

Ok("Bad status, check out <a href='http://help.us.com/'>our help page</a>").as(HTML)

Docs

Upvotes: 4

Related Questions