我想扩展内置的#{a
模板标记。例如,而不是每次都写:
#{a @Controller.Action(parameter.slugify()) /}
如果parameter="This Is a Test"
,则生成的href
为http://localhost/controller/action/this-is-a-test
我想对它进行扩展,以便默认情况下参数始终是slugified,而不是使用.slugify()
明确地执行它。
由于
答案 0 :(得分:3)
您要做的是定义自己的快速标签。有一个很好的例子here。 请将此答案检查为well。
代码可能如下所示:
@FastTags.Namespace("mytags")
public class MyTags extends play.templates.FastTags{
public static void _a(Map<?, ?> args, Closure body, PrintWriter out, ExecutableTemplate template, int fromLine) {
ActionDefinition actionDef = (ActionDefinition) args.get("arg");
if (actionDef == null) {
actionDef = (ActionDefinition) args.get("action");
}
if (!("GET".equals(actionDef.method))) {
if (!("POST".equals(actionDef.method))) {
String separator = actionDef.url.indexOf('?') != -1 ? "&" : "?";
actionDef.url += separator + "x-http-method-override=" + actionDef.method;
actionDef.method = "POST";
}
String id = Codec.UUID();
out.print("<form method=\"POST\" id=\"" + id + "\" " +(args.containsKey("target") ? "target=\"" + args.get("target") + "\"" : "")+ " style=\"display:none\" action=\"" + actionDef.url + "\">");
_authenticityToken(args, body, out, template, fromLine);
out.print("</form>");
out.print("<a href=\"javascript:document.getElementById('" + id + "').submit();\" " + serialize(args, "href") + ">");
out.print(JavaExtensions.toString(body));
out.print("</a>");
} else {
out.print("<a href=\"" + actionDef.url + "\" " + JavaExtensions.slugify(serialize(args, "href")) + ">");
out.print(JavaExtensions.toString(body));
out.print("</a>");
}
}
}
并在您的模板中这样调用:
#{mytags.a @Controller.Action(parameter) /}