3

Is there a way to include a plain html page inside a Play Framework's view template? I have a scenario wherein there is a common view template and in the body of the template, I would like to include certain static html pages. I know that I can include other templates inside a certain template, but I'm not sure if I could include a plain html page?

joesan
  • 13,963
  • 27
  • 95
  • 232

2 Answers2

5

One option is to just make your static HTML a template, eg, create myStaticPage.scala.html:

<h1>Static page</h1>
<p>This page is static, though it is still a template.</p>

Then your view template, myView.scala.html:

@(staticPage: Html)

<html>
  <head>...</head>
  <body>@staticPage</body>
</html>

And then in your action that renders the template:

def renderMyStaticPage = Action {
  Ok(views.html.myView(views.html.myStaticPage())
}

You just need to make sure that your HTML page escapes any @ symbols with @@.

On the other hand, if which HTML page that's being included is more dynamic, then simply load the HTML from the file/database/classloader/whereever it's coming from, eg:

def renderMyStaticPage = Action {
  val staticPage: String = // code to load static page here
  Ok(views.html.myView(Html(staticPage))
}
James Roper
  • 12,695
  • 46
  • 45
  • Is the val staticPage String refers to the location file path to where the static HTML is located or is it something else? – joesan Jan 02 '14 at 11:12
  • `val staticPage` in that example is just html as a string. Where you get that string is left to you. – Michael Zajac Jan 02 '14 at 12:24
1

You could.

Just put something like that in your routes file:

GET /file   controllers.Assets.at(path="/public", file="html/file.html")

Here is a duplicated post: Route to static file in Play! 2.0

Community
  • 1
  • 1
Christian Schmitt
  • 837
  • 16
  • 47