It's simple, simple ... create a view with the extension .js , i.e.: views/myDynamicScript.scala.js :
@(message: String) alert('@message');
So you can do it with Scala , like:
def myDynamicScript = Action { Ok(views.js.myDynamicScript.render(Hello Scala!")).as("text/javascript utf-8") }
or using Java :
public static Result myDynamicScript() { return ok(views.js.myDynamicScript.render("Hello Java!")); }
Create a route action for you (you might want to add some parameters to it):
GET /my-dynamic-script.js controllers.Application.myDynamicScript()
So you can include it in HTML templite, like:
<script type='text/javascript' src='@routes.Application.myDynamicScript()'></script>
Not necessary:
You can also display the script in your HTML document, i.e. by placing it in the <head>...</head> section:
<script type='text/javascript'> @Html(views.js.myDynamicScript.render("Find me in the head section of HTML doc!").toString()) </script>
Edit: @ See also samples for other types of templates
source share