0

I want to create js files using a template engine with Scala. Is it possible with the popular templating engines for Scala, namely Play and Scalate? If possible, than what are the pros and cons for using either of them?

Joan
  • 4,079
  • 2
  • 28
  • 37
Dondey
  • 267
  • 4
  • 13
  • Why would you want to create JS files with a template? – Luka Jacobowitz Mar 10 '16 at 09:16
  • Because the js file is composed of different static and user defined js snippets that need to be combined, which are project specific so values vary between projects, and has to be generated with up-to-date data. – Dondey Mar 10 '16 at 10:08

2 Answers2

1

Just create view with .js ext, i.e.: app/views/myScript.scala.js and dummy content:

@(message: String)

alert("@message");

Then add an action into your controller:

def myScript = Action {
  // use views.js... NOT views.html... !
  Ok(views.js.myScript.render("Whoha! I'm dynamic JS in Scala :)"))
}

or in Java version:

public Result myScript(){
    // use views.js... NOT views.html... !
    return ok(views.js.myScript.render("Yey! I'm dynamic JS in Java :)"));
}

Add the route to this action:

GET    /my-script    controllers.Application.myScript()

So you can use this route directly:

<script src="/my-script"></script> 

note, that Play should return valid Content-Type:text/javascript; charset=utf-8 in the response, anyway depending on version you are using it may be required to enforce this manually within your action (use browser's inspection tool to check the response type)

biesior
  • 55,576
  • 10
  • 125
  • 182
0

It really depends on what you want to achieve, i.e. how sophisticated your JavaScript code will be, but, unless it's something really small and simple, I'd suggest using the Scala.js. This way you basically will write some Scala code that will compile into JavaScript, and that compiled JavaScript you should be able to include into your Play application.

Advantages of writing Scala vs JavaScript should be pretty obvious (type safety, using lots of the existing Scala libraries). Disadvantage would be some delays for Scala -> JavaScript compilation, and also lack of the same seamless integration of Scala.js and Play, like Play has with its own templating engine. It's up to you to decide if the extra work to make these 2 work together worth it.

Haspemulator
  • 11,050
  • 9
  • 49
  • 76
  • Do you mean compile at runtime? Is that possible with scala.js? Anyway, I have to embed user-defined js snippets into the output, I want the static parts of the output to be precisely predictable, and I need minimal runtime-overhead so compilation might get in the way. So scala.js is not the path I'm looking at. Plus, I have bad memories from Script#. – Dondey Mar 10 '16 at 11:16
  • No, it's compiled with your project. I guess, I misunderstood your requirements. Scala.js is the way to go if you need to pre-package the JavaScript code with your build artifact, so, not your case. – Haspemulator Mar 10 '16 at 12:09