English 中文(简体)
How to define a tag with Play 2.0?
原标题:

There isn t much documentation about Play 2.0 template engine.

How does one create a tag using Scala template?

最佳回答

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:

  1. A level, represented by a string ( which defaults to "error")
  2. An index
  3. 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.

问题回答

Completely unnecessary answer but just to train my scala. Wouldn t this work and be shorter while staying clear?

@(level:String = "error", index: Int)(body: (String) => Html)

<p class="@level" index="@index">
    @body(
       @level match {
         case "success" => "green"
         case "warning" => "orange"
         case "error" => "red"
       }
    )
</p>

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

Let’s write a simple views/tags/notice.scala.html 
tag that displays an HTML notice:

@(level: String = "error")(body: (String) => Html)

@level match {

  case "success" => {
    <p class="success">
      @body("green")
    </p>
  }

  case "warning" => {
    <p class="warning">
      @body("orange")
    </p>
  }

  case "error" => {
    <p class="error">
      @body("red")
    </p>
  }

}

And now let’s use it from another template:

@import tags._
@notice("error") { color => Oops, something is wrong }





相关问题
Automatically pick tags from context using Python

How can I pick tags from an article or a user s post using Python? Is the following method ok? Build a list of word frequency from the text and sort them. Remove some common words and pick the top ...

Embed font into PDF file by using iText

I defined a tag map, and got a XML data file. I want to convert the XML data file to PDF by using iText. The question is how to embed fonts (e.g. Polish font, Chinese font) into the target PDF when ...

Splitting a string in C#

I was wondering if somebody could help me use string split to get all occurrences of text in between <p> </p> tags in an HTML document?

Tagging Posts via Wordpress XMLRPC

i am trying to publish a new post to a wordpress blog over the XMLRPC API. This works out fine so far, but now i want to add tags to the post. Either at creation time or afterwards. But i can t find ...

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Is it sometimes bad to use <BR />?

Is it sometimes bad to use <BR/> tags? I ask because some of the first advice my development team gave me was this: Don t use <BR/> ; instead, use styles. But why? Are there negative ...

iphone - Get dynamically created UITextField by tag

I add a UITextField to a table cell dynamically in my app. I d like to implement a "backgroundClick" method to dismiss the keyboard if the user presses outside the textfield (and outside the keyboard) ...

Re-insertion of <script> tags

Insertion of a file s tag, thus executing the file s code. Removal of the file s tag. Insertion of the same file s tag. Firebug does not seem to acknowledge and does not show the reinserted tag ...

热门标签