Playframework将放置在视图中呈现的文件 [英] Playframework were to put files that get rendered in views

查看:73
本文介绍了Playframework将放置在视图中呈现的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用playframework从视图内部的文件中渲染Asciidoc文本. 由于在我看来使用了该内容,因此我希望能够将其放入app/views中,以便在使用activator dist进行部署时将其打包.

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.

现在,运行activator dist后文件会丢失.因为内容是由我的视图呈现的,所以我不想放在public/app/assets中.

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

然后我的控制器将String内容发送到视图:

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?

推荐答案

您可以将内部"文档放在conf文件夹中,与标准sbt项目中的resources等效.

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

要访问它,请使用Play.resourceAsStream().请注意,这为您提供了java.io.InputStream,因为您的文件将成为activator dist创建的JAR的一部分.

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)

这篇关于Playframework将放置在视图中呈现的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆