Playframework: Custom template path

匆匆过客 提交于 2019-12-25 07:38:17

问题


I was wondering if it is possible to override/add to the template paths for Playframework? For instance, the default templates are looked under views folder and are compiled automatically. These templates are further reachable directly using a URL without any additional config.

---app
   |-controllers
   |-models
   |-templates  //How do I compile templates under this folder?
   |-views

What I would like to know is if it is possible to add a custom path in addition to the views folder that is also compiled with the build process. Alternatively, is it possible to block certain templates to be not reachable by direct URL ?

Thanks in advance!


回答1:


Under the app directory, Play should automatically compile anything that looks like a Twirl template - that is, has a registered extension such as *.scala.html - regardless of what directory it's in. The views directory is just a convention, and compiling templates under an app/templates directory should already happen.

If you open an SBT prompt you can verify this by running the command:

show twirlCompileTemplates::sourceDirectories

Which should give you something like:

[info] my-project/compile:twirlCompileTemplates::sourceDirectories
[info]  List(/home/me/my-project/app)

For adding a templates directory outside the app folder, you should be able to add something like the following in your build.sbt (for example, the directory extra_templates):

import play.twirl.sbt.Import.TwirlKeys._

sourceDirectories in (Compile, compileTemplates) += file("extra_templates")


来源:https://stackoverflow.com/questions/38385904/playframework-custom-template-path

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