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

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

问题描述

使用播放框架创建自定义标签的方法有两种.

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

  1. 通过在 app/view/tags 中定义一个 groovy 模板
  2. 通过类扩展 FastTags 直接在纯 Java 中

最新的没有记录.

推荐答案

因此,类似于 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));
}

所以,第一个方法是verbatim 标签,它简单地调用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天全站免登陆