Reputation: 31
Say I have the following view files: dashboard.scala.html, index.scala.html, main.scala.html and user.scala.html. And the following javascript files: dashboard.js, user.js and main.js
Is there a way to only have the user.js file load when the user.scala.html view is loaded, the dashboard.js file only load on the dashboard.scala.html view and not on the other view pages? Currently all my view files are run through the main.scala.html file which has all my css and javascript includes.
I was thinking i need a way to pass the current view name to the main.scala.html file and then append the view name in the script src attribute or is there a way to do this thru the routes file? Also im using the Java API for this application.
<script src="@routes.Assets.at("javascripts/vendor/jquery.min.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/main.js")" type="text/javascript"></script>
<script src="@routes.Assets.at("javascripts/" + viewName + ".js")" type="text/javascript"></script>
Upvotes: 2
Views: 977
Reputation: 55798
In Play 1.x there was two special tags for including additional scripts and styles from the view to the wrapping layout: moreScripts
and moreStyles
, anyway they doesn't exists anymore...
Fortunately you can do the same manually in Play 2, and what's best they are not fixed anymore, you can just create as many such blocks as required.
Check templates' documentation, search for "moreScripts and moreStyles equivalents" section.
On the other hand, sometimes it's easier to prepare separate 'layout' with scripts and styles in required order for given view(s).
Upvotes: 3