Playframework were to put files that get rendered

2019-09-18 14:32发布

问题:

I am using the playframework to render Asciidoc text from a file inside my view. Since that content is used in my view, I want to be able to put it in the app/views so it gets packaged when deploying with activator dist.

Right now the files get lost after running activator dist. Because the content gets rendered by my view I don't want to put in in public/ or in app/assets.

My view looks versy simple:

@(html: String)(implicit flash: Flash, lang: Lang)

@main(Messages("application.name")){
  @Html(html)
}

And my controller sends the String content to the view:

def about = Action { implicit request =>
  Ok(views.html.statics.normal(Static.render_file("app/views/adoc/about.adoc")))
}

Where should I put this file? and how to I access it other than with the path from the root?

回答1:

You can put "internal" documents in the conf folder, it's the equivalent to resources in standard sbt projects.

To access it, you'd use Play.resourceAsStream(). Note that this gives you an java.io.InputStream because your file will be part of the JAR created by activator dist.

Play.resourceAsStream("adoc/about.adoc") map { adocStream =>
  Ok(views.html.statics.normal(Static.render_file(adocStream)))
} getOrElse (InternalServerError)