最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Play Framework template that is actually a JS file - Stack Overflow

programmeradmin4浏览0评论

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).

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).

Share Improve this question edited May 23, 2017 at 12:17 CommunityBot 11 silver badge asked Nov 5, 2014 at 0:13 MikeMike 7972 gold badges9 silver badges17 bronze badges 2
  • You can define your javascript files as Play templates. Just like you do for html templates. You only need to add your template and render it from a controller and add a route. But always there are better ways than creating a dynamic javascript. As for caching, what if you create a normal javascript file and serve it with play? You think it won't be cached? Why? – Nader Ghanbari Commented Nov 5, 2014 at 6:17
  • @NaderHadjiGhanbari, I didn't say that a normal JS file wouldn't be cached. Rather, I need to be able to substitute data into the file with Play and serve an actual JS file and not embedded JS code (in the HTML). – Mike Commented Nov 6, 2014 at 17:16
Add a ment  | 

1 Answer 1

Reset to default 19

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

发布评论

评论列表(0)

  1. 暂无评论