Custom view directories for Play Framework 2.x not

2019-09-03 06:39发布

问题:

I'm new to using Play Framework (2.2.x) and I am having a tough time figuring out how to render views not located in the \app\views directory.

My project is organized such that each section (directory) of the app has it's own views, controllers, services, repositories... etc. It's a structure I'd like to keep. It looks something like this:

\app\Game\Views
\app\Game\Controllers
\app\Game\Services
\app\Game\Repositories

\app\Players\Views
\app\Players\Controllers

\app\Common\Views
\app\Common\Repositories
... etc

Views, as you can see from above, are in sub-directories in each app section. View locations vary from one sub-directory deep to many more. It looks something like this:

\app\Game\Views
\app\Game\Views\sports.scala.html
\app\Game\Views\dates.scala.html
\app\Game\Views\invites.scala.html

\app\Common\Views\Layouts
\app\Common\Views\Layouts\default.scala.html
\app\Common\Views\Partials
\app\Common\Views\Partials\Headers\default.scala.html
\app\Common\Views\Partials\Headers\onboarding.scala.html
... etc

If I follow the default 'Play' way of rendering a view located in \app\views everything works fine:

views.html.index.render()

I'm having a tough time adapting the default 'Play' way to recognize views located outside of the \app\views directory. I'm sure there's some sort of configuration or way of identifying those views, but I just can't see it in the documentation.

How do I render a view not located in the \app\views directory?

Thanks for your help.

回答1:

You don't have to do much, but just address it correctly.

Assume you have your view in app\common\views\index.scala.html, then in your controller you have to refer to it common.views.html.index. Note the extra common.

You can check the actual packages of the classes generated from the templates by looking into target/scala-2.10/src_managed/main/<your-prefixes>/views/html/