Play Framework template that is actually a JS file

Mike

I'd like to have a Play template that is a JS file (as opposed to having <script> tags inside an HTML template). The reason for this is so that the script can be cached. However, I need to create a differences in the script depending on where it's included and hoped to do this with Play's template system. I can already do so if I use embedded scripts, but those can't be cached.

I found an existing question that also asks the same thing, but the answer is totally different (different goals).

biesior

That's easy, just... create view with .js extension, i.e.: views/myDynamicScript.scala.js:

@(message: String)

alert('@message');

//Rest of your javascript...

So you can render it with Scala action as:

def myDynamicScript = Action {
  Ok(views.js.myDynamicScript.render(Hello Scala!")).as("text/javascript utf-8")
}

or with Java action:

public static Result myDynamicScript() {
    return ok(views.js.myDynamicScript.render("Hello Java!"));
}

Create the route to you action (probably you'll want to add some params to it):

GET   /my-dynamic-script.js      controllers.Application.myDynamicScript()

So you can include it in HTML templite, just like:

<script type='text/javascript' src='@routes.Application.myDynamicScript()'></script>

Optionally:

You can also render the script into your HTML doc, ie by placing this in your <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 templates types

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

When actually file upload done in MultipartFormData in Play Framework

From Dev

Play Framework Scala Template

From Dev

Play! Framework FakeApplication - What does it actually do?

From Dev

Play! Framework FakeApplication - What does it actually do?

From Dev

Play framework dynamic template include

From Dev

Play framework dynamic template include

From Dev

Play framework + Processing js

From Dev

Play Framework: How do I get querystring value inside html template file

From Dev

Bounded generic parameters in Play Framework template

From Dev

How to inject Application in Play Framework 2.5 template

From Dev

Display Dynamic Image in Play! Framework Template

From Dev

Get scala template in a variable in Play Framework

From Dev

play framework passing sub class to scala template

From Dev

Play Framework template, iterate backwards over List

From Dev

using bootstrap with play framework scala template

From Dev

Play2 Framework Float Format in Template

From Dev

Play framework scala template with case statement on enumeration

From Dev

Get Javascript variable in Template play framework 1.2.6

From Dev

play framework passing sub class to scala template

From Dev

using bootstrap with play framework scala template

From Dev

Referencing template syntax system in Play framework

From Dev

Play! Framework - return multiple list to template

From Dev

Microservices template project for Play Framework with java

From Dev

Play Framework Error in Routes file

From Dev

Play framework file hierarchy on Heroku

From Dev

Pass reference to a template class in Play2 framework template engine

From Dev

Play framework file upload no such file exception

From Dev

How to read a file in Play Framework 2.2.1?

From Dev

Loading an arbitrary file in the Play Framework 2 (Scala)

Related Related

  1. 1

    When actually file upload done in MultipartFormData in Play Framework

  2. 2

    Play Framework Scala Template

  3. 3

    Play! Framework FakeApplication - What does it actually do?

  4. 4

    Play! Framework FakeApplication - What does it actually do?

  5. 5

    Play framework dynamic template include

  6. 6

    Play framework dynamic template include

  7. 7

    Play framework + Processing js

  8. 8

    Play Framework: How do I get querystring value inside html template file

  9. 9

    Bounded generic parameters in Play Framework template

  10. 10

    How to inject Application in Play Framework 2.5 template

  11. 11

    Display Dynamic Image in Play! Framework Template

  12. 12

    Get scala template in a variable in Play Framework

  13. 13

    play framework passing sub class to scala template

  14. 14

    Play Framework template, iterate backwards over List

  15. 15

    using bootstrap with play framework scala template

  16. 16

    Play2 Framework Float Format in Template

  17. 17

    Play framework scala template with case statement on enumeration

  18. 18

    Get Javascript variable in Template play framework 1.2.6

  19. 19

    play framework passing sub class to scala template

  20. 20

    using bootstrap with play framework scala template

  21. 21

    Referencing template syntax system in Play framework

  22. 22

    Play! Framework - return multiple list to template

  23. 23

    Microservices template project for Play Framework with java

  24. 24

    Play Framework Error in Routes file

  25. 25

    Play framework file hierarchy on Heroku

  26. 26

    Pass reference to a template class in Play2 framework template engine

  27. 27

    Play framework file upload no such file exception

  28. 28

    How to read a file in Play Framework 2.2.1?

  29. 29

    Loading an arbitrary file in the Play Framework 2 (Scala)

HotTag

Archive