【问题标题】:Include plain HTML page inside a Play Framework view template在 Play Framework 视图模板中包含纯 HTML 页面
【发布时间】:2014-01-19 08:45:24
【问题描述】:

有没有办法在 Play Framework 的视图模板中包含一个纯 html 页面?我有一个场景,其中有一个通用视图模板,并且在模板的正文中,我想包含某些静态 html 页面。我知道我可以在某个模板中包含其他模板,但我不确定是否可以包含纯 html 页面?

【问题讨论】:

标签: html playframework


【解决方案1】:

一种选择是将静态 HTML 设为模板,例如,创建 myStaticPage.scala.html

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

然后是你的视图模板,myView.scala.html:

@(staticPage: Html)

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

然后在您呈现模板的操作中:

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

您只需要确保您的 HTML 页面使用 @@ 转义任何 @ 符号。

另一方面,如果所包含的 HTML 页面更具动态性,则只需从文件/数据库/类加载器/无论它来自何处加载 HTML,例如:

def renderMyStaticPage = Action {
  val staticPage: String = // code to load static page here
  Ok(views.html.myView(Html(staticPage))
}

【讨论】:

  • val staticPage String是指静态HTML所在的位置文件路径还是别的什么?
  • val staticPage 在该示例中只是 html 作为字符串。你在哪里得到那个字符串是留给你的。
【解决方案2】:

你可以。

只需在你的路由文件中添加类似的内容:

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

这是一个重复的帖子:Route to static file in Play! 2.0

【讨论】:

  • 我不认为张贴者想要提供静态 HTML,而是希望将 HTML 加载到模板中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-21
  • 1970-01-01
  • 2011-08-18
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 2014-05-11
相关资源
最近更新 更多