有人可以解释如何使用FastTags [英] Can someone explain how to use FastTags

查看:73
本文介绍了有人可以解释如何使用FastTags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用播放框架创建海关标签有两种方法。

There are two ways to create customs tags with the play framework.


  1. 通过在app / view / tags中定义groovy模板

  2. 直接在纯java中通过类扩展FastTags

最新版本没有记录。

推荐答案

因此,类似于JavaExtensions如何通过扩展JavaExtensions类来创建FastTag,您需要创建一个扩展的类FastTags。您要作为标记执行的每个方法都需要符合以下方法结构。

So, similar to how JavaExtensions work by extending the JavaExtensions class, to create a FastTag you need to create a class that extends FastTags. Each method that you want to execute as a tag needs to conform to the following method structure.

public static void _tagName(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine)

注意标记名称前面的下划线。

要了解如何构建实际标记,最简单的方法是查看源代码一个FastTag,看看其中一个。

To understand how to build an actual tag, the easiest way is to look at the source code for a FastTag, and see one in action.

以下是来自git hub的源代码。
https://github.com /playframework/play/blob/master/framework/src/play/templates/FastTags.java

Here is the source straight from git hub. https://github.com/playframework/play/blob/master/framework/src/play/templates/FastTags.java

以下是我复制的一些内容,以便我可以解释这是如何工作的。

Below are a few I have copied, so that I can explain how this works.

public static void _verbatim(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
    out.println(JavaExtensions.toString(body));
}

所以,第一种方法是逐字标签,并简单地在JavaExtensions上调用toString方法,并传入标记的主体。标签的主体可以是打开和关闭标签之间的任何内容。所以

So, this first method is the verbatim tag, and simply calls the toString method on the JavaExtensions, and passes in the body of the tag. The body of the tag would be anything between the open and close tag. So

<verbatim>My verbatim</verbatim>

正文值为

My verbatim

第二个例子稍微复杂一点。它是依赖父标签运行的标签。

The second example, is slightly more complex. It is a tag that relies on a parent tag to function.

public static void _option(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
    Object value = args.get("arg");
    Object selectedValue = TagContext.parent("select").data.get("selected");
    boolean selected = selectedValue != null && value != null && selectedValue.equals(value);
    out.print("<option value=\"" + (value == null ? "" : value) + "\" " + (selected ? "selected=\"selected\"" : "") + "" + serialize(args, "selected", "value") + ">");
    out.println(JavaExtensions.toString(body));
    out.print("</option>");
}

此代码的工作原理是输出HTML选项标签,并设置所选值,通过检查从父标签中选择的值。前3行只获取数据,并设置准备输出的数据。然后,最后3行输出标记的结果。

This code works by outputting an HTML option tag, and sets the selected value, by checking which value is selected from the parent tag. The first 3 lines just get data, and set up the data ready to output. Then, the final 3 lines outputs the result of the tag.

我链接到的源代码中有更多的例子,具有不同程度的复杂性,但希望如此这对你来说是一个很好的起点。

There are many more examples in the source code I have linked to, with varying degrees of complexity, but hopefully this will be a good starting point for you.

为确保你的标签不会在项目之间发生冲突,或者与核心Play标签发生冲突,你可以设置名称空间,使用类级别注释@ FastTags.Namespace。

To ensure that your tags do not conflict between projects, or with the core Play tags, you can set up namespaces, using the class level annotation @FastTags.Namespace.

因此,对于hello标记,在my.tags的命名空间中,您将执行以下操作

So, for a hello tag, in a namespace of my.tags, you would do the following

@FastTags.Namespace("my.tags") 
public class MyFastTag extends FastTags {
    public static void _hello (Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
        ...
    }
}

然后在你的模板中,你会将hello标记引用为

and then in your templates, you would reference the hello tag as

#{my.tags.hello/}

这篇关于有人可以解释如何使用FastTags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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