Include plain HTML page inside a Play Framework view template

放肆的年华 提交于 2020-01-02 20:08:22

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/20873866/include-plain-html-page-inside-a-play-framework-view-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!