开发者

Passing action plus appended string to tag in Play

I'm trying to pass an action to a custom tag, but instead of just the bare action, I also want to append a string to the generated url.

With just an action it works:

#{nav.menu label: 'Log In', url: @access.AccessControl.login() /}

But #{nav.menu label: 'Log In', url: @access.AccessControl.login() + '?url=' + request.url /} results in

MissingMethodException : No signature of method: play.mvc.Router$ActionDefinition.plus() is applicable for argument types: (java.lang.String) values: [?url=] Possible solutions: use([Ljava.lang.Object;), split(groovy.lang.Closure), is(java.lang.Object), wait(), any(), dump().

When I try #{nav.menu label: 'Log In', url: @{access.AccessControl.login()} + "?url=" + request.url /}, it says #{nav.menu} is not closed}:

So then I tried

%{ loginPath = @access.AccessControl.login(); }%
#{nav.menu label: 'Log In', url: loginPath + "?url=" + request.url /}

but then it says "The template /app/views/main.html does not compile : unexpected token: @ "

If I do this, the action isn't replaced with a path:

%{ loginPath = '@{access.AccessControl.login()}' }%
#{nav.menu label: 'Log In', url: loginPath + "?url=" + request.url /}

So I'm pretty mu开发者_开发技巧ch out of ideas. What does it want from me?


I've been able to get it working using the following but it seems like there must be a better way:

#{nav.menu label: 'Log In', url: play.mvc.Router.reverse('access.AccessControl.login').url + '?url=' + request.url /}


you can pass url parameter with .add(paramName, paramValue)

for example, I'll define my own anchor tag like this

views/tags/anchor.html

%{
action = anchor ?: _arg;
%}
#{a action}my own anchor#{/a}

and I can use it like:

#{anchor @Application.newAction().add('name', 'john').add('nick', 'johnny') /}<br />

this would generate the following html

<a href="/application/newaction?nick=juancho&amp;name=juan">mi propio anchor</a>


Have you tried

%{ url = request.url }%
#{nav.menu label: 'Log In', url: @access.AccessControl.login(url) /}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜