Custom view directories for Play Framework 2.x not working

為{幸葍}努か 提交于 2020-01-16 23:15:28

问题


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/



来源:https://stackoverflow.com/questions/23724633/custom-view-directories-for-play-framework-2-x-not-working

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