Reputation: 1038
I've been using Play 2.0 framework for a couple of days now for a proof of concept application at my job. One of the first things I wanted to check out was the custom tag functionality, since it reminded me of HtmlHelpers in ASP.Net MVC. The thing is that I can't seem to make them work and was wondering if I'm misusing the feature or misunderstanding something.
Here's a simple example of what I want to do: I want to be able to use @script("scriptname.js") anywhere in the templates and have that subsitute the entire tags.
Here's what I got so far:
main.scala.html
@(title: String, scripts: Html = Html(""))(content: Html)
@import tags._
<!DOCTYPE html>
<html>
<head>
<!-- this is how I would like to use the helper/tag -->
@script("jquery.js")
@script("jquery-ui.js")
<!-- let views add their own scripts. this part is working OK -->
@scripts
</head>
<body>
@content
</body>
</html>
I created a subdirectory called "tags" under the app/views directory. There I created my script.scala.html tag/helper file:
@(name: String)
<script src="@routes.Assets.at("javascripts/@name")" type="text/javascript"></script>
The problem I'm having is that whenever I use @script() the output includes the @name parameter in it. For example @script("x.js") actually outputs
<script src="assets/javascripts/@name" type="text/javascript"></script>
What am I doing wrong?
For the record, I did read the documentation and search here, but neither of these links have helped me figure this out:
http://www.playframework.org/documentation/2.0.3/JavaTemplateUseCases
How to define a tag with Play 2.0?
Upvotes: 1
Views: 1354
Reputation: 8895
@routes.Assets.at(...)
evaluates the Scala expression routes.Assets.at(...)
and substitutes the result into your output. There is no recursive evaluation that would allow you to have evaluate an expression textually to get that expression, which seems to be what you're expecting.
What you intend to do is achieved using
@routes.Assets.at("javascripts/" + name)
Upvotes: 3