javascripthtmlplayframework-2.0twirl

Play Framework 2.5 using JS file in index.scala.html


I'm currently attempting to refactor my repeated JavaScript code from my xyz.scala.html files into a separate main.js file, which can then be linked to using something like the below (from my scala.html files):

<script src='@routes.Assets.at("javascripts/main.js")'><script>

And using the below in my conf/routes file:

GET     /js/*file               controllers.Assets.at(path = "/public/javascripts", file)

Having Googled around I'm struggling to find a simple way of using JS from a file in my Play Framework application and setting up the conf/routes file.

Does anyone know a simple way of getting this to work?


Solution

  • Public assets folders

    Public assets folders

    Record in the routes file

    GET     /assets/*file               controllers.Assets.versioned(path="/public", file: Asset)
    

    Script loading in twirl template

    <script src="@routes.Assets.versioned("javascripts/hello.js")" type="text/javascript"></script>