Вопрос

I'd like to extend the built in #{a template tag. For example, instead of writing every time:

#{a @Controller.Action(parameter.slugify()) /}

Where if parameter="This Is a Test" then the generated href is http://localhost/controller/action/this-is-a-test

I'd like to extend it so that the parameter is always slugified by default without explicitly doing it with .slugify().

Thanks

Это было полезно?

Решение

What you want to do is define your own fast tags. There is a good example here. Check this answer as well.

The code would probably look like this:

@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>");
        }
    }
}

and be called like this in your template:

#{mytags.a @Controller.Action(parameter) /}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top