如何使用Play 2.0定义标签? [英] How to define a tag with Play 2.0?

查看:104
本文介绍了如何使用Play 2.0定义标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于Play 2.0模板引擎的文档很少.

如何使用Scala模板创建标签?

解决方案

play 2.0中的模板引擎直接来自play 1.0 scala模块.如果您仍然想知道诸如Scala之类的功能语言给图片带来了什么好处,那么无疑这就是它发挥作用的领域之一.

演示:

在scala语法中,标记不过是函数调用.有趣的是,html片段本身被视为函数,允许使用最强大的替换构造.

让我们定义一个名为 mytag.scala.html

的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>
    }    
}

上面的标记在2个不同的参数组中包含3个参数:

  1. 一个级别,由字符串表示(默认为错误")
  2. 索引
  3. 最后一个叫做body的函数,它带有一个字符串参数并返回HTML代码.请注意,主体是在其自己的参数组中定义的.它等同于我们在j2ee中所知道的 jsp片段.

现在让我们看看如何使用此标记:

@import views.mytags._

@mytag("error",2) { color =>
    Oops, something is <span style="color:@color">wrong</span>
}

在使用标签(或函数)之前,我们需要让Play知道它的位置:这就是 import 语句的目的.请注意,就像您对Java软件包一样,只要您调整导入位置,标记文件的位置(路径)就无关紧要.

遵循呼叫本身,这很简单.但是请注意,我们正在将参数化的html片段传递给标记.

有关更多详细信息,您可以在以下 URL

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.

这篇关于如何使用Play 2.0定义标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆