The template engine in play 2.0 is directly coming from the play 1.0 scala module. If you are still wondering what benefits does a functional language such as Scala brings to the picture, well this is certainly one of the areas where it shines.
Demonstration:
In scala syntax a tag is nothing else than a function call. what's interesting, is that html fragments are considered as functions themselves, allowing the most powerful substitution constructs.
The tag above takes 3 parameters in 2 distinct parameter groups:
A level, represented by a string ( which defaults to "error")
An index
Finally a function called body, that takes a string parameter and returns HTML code. Note that body is defined in its own parameter group. it is equivalent to what we know in j2ee as a jsp fragment.
Now let's see how we can use this tag:
@import views.mytags._
@mytag("error",2) { color =>
Oops, something is <span style="color:@color">wrong</span>
}
Before we can use a tag (or function), we need to let Play know where it is located: that's the purpose of the import statement. Note that the location (the path) of the tag file is irrelevant as long as you adjust the import location, just like with Java packages.
Follows the call itself which is kind of straightforward. Note however that we are passing a parametrized html fragment to the tag.
For further details, you may find the scala template documentation at this URL
Play 2.0 will eventually come with its own documentation.
The template engine in play 2.0 is directly coming from the play 1.0 scala module. If you are still wondering what benefits does a functional language such as Scala brings to the picture, well this is certainly one of the areas where it shines.
Demonstration:
In scala syntax a tag is nothing else than a function call. what's interesting, is that html fragments are considered as functions themselves, allowing the most powerful substitution constructs.
Let's define an html page called mytag.scala.html
file:apps/views/mytags/mytag.scala.html
The tag above takes 3 parameters in 2 distinct parameter groups:
Now let's see how we can use this tag:
Before we can use a tag (or function), we need to let Play know where it is located: that's the purpose of the import statement. Note that the location (the path) of the tag file is irrelevant as long as you adjust the import location, just like with Java packages.
Follows the call itself which is kind of straightforward. Note however that we are passing a parametrized html fragment to the tag.
For further details, you may find the scala template documentation at this URL
Play 2.0 will eventually come with its own documentation.
Completely unnecessary answer but just to train my scala. Wouldn't this work and be shorter while staying clear?
i get a compiler error, when I used the first example. Delete the "views." in the import solved the problem
use @import mytags._
Full example (http://www.playframework.com/documentation/2.1.1/JavaTemplateUseCases):