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

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

问题描述

关于 Play 2.0 模板引擎的文档并不多.

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

解决方案

play 2.0中的模板引擎直接来自play 1.0 scala模块.如果您仍然想知道像 Scala 这样的函数式语言能给图片带来什么好处,那么这肯定是它的亮点之一.

演示:

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

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

的 html 页面

文件:apps/views/mytags/mytag.scala.html

@(level:String = "error", index: Int)(body: (String) => Html)@level 匹配 {案例成功"=>{<p class="成功" index="@index">@body("绿色")</p>}案例警告"=>{<p class="警告" index="@index">@body("橙色")</p>}案例错误"=>{<p 类="错误" 索引="@index">@body("红色")</p>}}

上面的标签采用 2 个不同参数组中的 3 个参数:

  1. 一个级别,由一个字符串表示(默认为error")
  2. 索引
  3. 最后是一个名为 body 的函数,它接受一个字符串参数并返回 HTML 代码.请注意,body 是在其自己的参数组中定义的.它相当于我们在 j2ee 中所知道的 jsp 片段.

现在让我们看看如何使用这个标签:

@import views.mytags._@mytag("错误",2) { 颜色 =>糟糕,有些东西是<span style="color:@color">错误</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天全站免登陆