Automatically inject WebJarAssets in a Play 2.5 HTML template?

In my HTML template to play inside my custom module, I have the following line of code:

 <script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script> 

This refers to the WebJarAssets class in the core module, which looks like this:

 package controllers.core import javax.inject._ import play.api.http.HttpErrorHandler import play.api.{Environment, Configuration} class WebJarAssets @Inject()(errorHandler: HttpErrorHandler, configuration: Configuration, environment: Environment) extends controllers.WebJarAssets(errorHandler, configuration, environment) 

Note that I also included the following line in build.sbt in the custom module:

 "org.webjars" %% "webjars-play" % "2.5.0", 

When starting the application, the following error appears:

 [error] /Users/john/DemoProject/modules/custom/app/views/custom/templates/main.scala.html:36: not found: value WebJarAssets [error] <script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(WebJarAssets.locate("jquery.min.js"))"></script> [error] ^ 

Judging by the implementation of WebJarAssets in earlier versions, there is no need to implement and introduce your own controller for this (for example, see this tutorial ).

What am I doing wrong? I even need a WebJarAssets class for this (since I need web banner resources in many modules, I added this now in the core module). Also, why do I need to manually enter it into the template and why is it not allowed automatically?

In case you are wondering, what does it enter for me manually:

 @(title: String, webJarAssets: WebJarAssets)(content: Html)(implicit messages:Messages) <script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(webJarAssets.locate("jquery.min.js"))"></script> 
+5
source share
3 answers

Prior to webjars-play 2.5.0 a static method appeared that you could use in your templates. Since Play is moving away from global states / static methods, this has been removed from webjars-play , and now you need to enter WebJarAssets . Unfortunately, Twirl patterns do not support injection. Therefore, you must enter it into your controller, and then transfer it to your template. Here is a complete sample application that does this: https://github.com/webjars/webjars-play/tree/master/test-project

+7
source

He can write like that.

controller:

 class Application @Inject()(implicit webJarAssets: WebJarAssets, val messagesApi: MessagesApi, materializer: Materializer) extends Controller with I18nSupport { 

template:

 @(title: String)(content: Html)(implicit messages: Messages, webJarAssets: WebJarAssets) <script type='text/javascript' src='@routes.WebJarAssets.at(webJarAssets.locate("jquery.min.js"))'></script> 
+2
source

As @ james-ward said in play-2.5.12 with twirl-1.2.0 , you can declare dependencies in templates, but they will become classes and not objects, which complicates the link to templates from others.

Changes:

Project /plugins.sbt

 addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.2.0") 

main.scala.html

 @this(webJarAssets: WebJarAssets) @(title: String)(content: Html)(implicit messages:Messages) <script type="text/javascript" src="@controllers.core.routes.WebJarAssets.at(webJarAssets.locate("jquery.min.js"))"> </script> 

Note that the caller of the main template must also declare the constructor mainClient.scala.html :

 @this(mainRef: main) @(...) content @main("title"){ } content 
+1
source

All Articles