Search code examples
playframework-2.0twirl

Moving PlayFramework 2.4.x View Templates into a specific Package


I'm on IntelliJ 2.4.x and I'm trying to move the Twirl view template into a specific package (not the default package views.html). The reason for this is I have a sub project and I'd like to avoid clashing view names by namespacing the two sets of views differently. Is there anyway I can do that?


Solution

  • To avoid namespace clash just add standard namespace folders layout into views directory:

    views +
          L com +
                L yourdomain + 
                             L yourTemplateFile.scala.html
    

    Call it from a controller as:

    object Application extends Controller {
      def = Action {
          Ok( views.html.com.yourdomain.yourTemplateFile() )
      }
    }
    

    See Twirl (Play template engine) project for more details.

    According to documentation extensions can be: html, js, xml, or txt.

    The above works fine in Play 2.4 whether your use Play application layout or SBT Maven layout as described in https://playframework.com/documentation/2.4.x/Anatomy

    You can check SBT Maven layout test in a basic project I play with on github: psmaster1. Here Twirl/Play templates are located in src/main/twirl/views