Play Framework 2.4.x module/sub-project assets
As I was working on creating a Play Module, I wanted to make reference of the module assets though the routes. This is so the new module can expose its own assets. I tried to follow the Play 2.4 SBTSubProjects instructions and wasn’t able to get the routing working correctly. I decided to inspect the code generated by sbt until I was able to get it working correctly.
Following the How to create module with Play Framework instructions from the previous post, all you need to do is create an Assets class inside of your module controller package and make reference of it from the module routing.
Your module’s Assets class would look something like this:
package sample_module.controllers; import com.google.inject.Inject; import play.api.mvc.Action; import play.api.mvc.AnyContent; /** * Created by lcamilo on 7/26/15. */ public class Assets { @Inject controllers.Assets assets; public Action<AnyContent> versioned(String path, controllers.Assets.Asset file) { return assets.versioned(path, file); } }
Your module routing file would look something like this:
# module/app/conf/sample_module.routes # Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET /sample_module/ @sample_module.controllers.Application.index() # Map static resources from the /public folder to the /assets URL path GET /sample_module/*file @sample_module.controllers.Assets.versioned(path="/public/lib/sample_module", file)
Now just include the sub-module routing file from the base project
# app/conf/routes # Routes # This file defines all application routes (Higher priority routes first) # ~~~~ # Home page GET / @controllers.Application.index() #Add sample_module routes -> / sample_module.Routes # Map static resources from the /public folder to the /assets URL path GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
If you followed the previous post of How to create module with Play
Framework your folder structure would look something like :
├── app │ ├── controllers │ │ └── Application.java │ └── views │ ├── index.scala.html │ └── main.scala.html ├── conf │ ├── application.conf │ ├── logback.xml │ └── routes ├── module │ ├── app │ │ └── sample_module │ │ ├── controllers │ │ └── views │ ├── public │ │ ├── images │ │ ├── javascripts │ │ └── stylesheets │ ├── ... ├── ...
Now lets see how we use the assets from the module views :
From your views you can make reference of the newly added routes :
#file : module/app/sample_module/views/sample_module_main.scala.html @(title: String)(content: Html) <!DOCTYPE html> <html lang="en"> <head> <title>@title</title> <link rel="stylesheet" media="screen" href="@sample_module.controllers.routes.Assets.versioned("stylesheets/main.css")"> <link rel="shortcut icon" type="image/png" href="@sample_module.controllers.routes.Assets.versioned("images/favicon.png")"> <script src="@sample_module.controllers.routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script> </head> <body> @content </body> </html>
The source code for the play-module-sample can be found here.