WebJars making views uselessly complicated with injection in Play2.5

102 views
Skip to first unread message

Janiga

unread,
Jan 15, 2017, 10:44:59 PM1/15/17
to Play Framework
From:


public class Application extends Controller {
 
@Inject WebJarAssets webJarAssets;
 
public Result index() {
 
return ok(index.render(webJarAssets)); }
}

@(webJarAssets: WebJarAssets) <!DOCTYPE html> <html> <head> <link rel='stylesheet' href='@routes.WebJarAssets.at(webJarAssets.locate("css/bootstrap.min.css"))'> </head> <body> </body> </html>


This is making only using templates more complicated , since you need to pass another parameter. And you probably use all this WebJarAssets at whole application (probably not only in REST service), so you will pass this as parameter to every template you have. Imagine having another parameters , then how many parameters you pass around templates ,even if you did not had to in past. I really do not like this approach making things more complicated and unreadable..

Is there any better approach? Calling action with every controller , which will put webjars in context and then in particular view reading it from context is only I managed to come up with, but still making things more complicated even if they do not have to be...

Will Sargent

unread,
Jan 17, 2017, 9:56:04 PM1/17/17
to play-fr...@googlegroups.com
You do not have to use Webjars at all, if you don't want to.  CSS and Javascript can be managed outside of Play if that suits your needs better.

--
Will Sargent
Engineer, Lightbend, Inc.


--
You received this message because you are subscribed to the Google Groups "Play Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framework+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/6bb8fb06-8672-4e99-ada6-8ad1558d9b7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

James Ward

unread,
Jan 20, 2017, 4:10:34 PM1/20/17
to Play Framework
Unfortunately WebJarAssets needs some stuff injected:

So if you want to use WebJarAssets in your templates then you need to get it there.  Alternatively you can not use it and instead use sbt-web:

Or you can use Twirl template injection but this requires a newer version of Twirl:

And some config:
https://github.com/webjars/webjars/blob/master/build.sbt#L11

Then you can inject WebJarAssets:

(In my case I'm injecting a wrapper around WebJarAssets.)

Hope that helps.

-James
Reply all
Reply to author
Forward
0 new messages