Playframework: Custom template path

2019-09-14 03:15发布

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条回答
Juvenile、少年°
2楼-- · 2019-09-14 03:53

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")
查看更多
登录 后发表回答