There isn't much documentation about Play 2.0 template engine.
How does one create a tag using Scala template?
A placement tag—sometimes called an ad tag—is code that calls an ad server for ad content when users visit a site. Campaign Manager 360 serves ads when users visit a site with Campaign Manager 360 placement tags.
Campaign Manager 360 (CM360) Click tags will work for Website traffic, Video views, and Pre-roll views. Under “Ad group details” > “Delivery”, click on “Measurement options” to expand additional fields.
The DoubleClick Conversion tag lets you collect data about visitors and send it to DoubleClick. DoubleClick is then able to show visitors ads that match their interests. In Tag Manager, you can set up the tag in a hassle-free way.
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
@(level:String = "error", index: Int)(body: (String) => Html)
@level match {
case "success" => {
<p class="success" index="@index">
@body("green")
</p>
}
case "warning" => {
<p class="warning" index="@index">
@body("orange")
</p>
}
case "error" => {
<p class="error" index="@index">
@body("red")
</p>
}
}
The tag above takes 3 parameters in 2 distinct parameter groups:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With