1

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>")
  }
}

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.