A couple of things about reverse routing in templates

670 views
Skip to first unread message

Alberto Gimeno

unread,
Jan 8, 2010, 6:25:32 AM1/8/10
to play-framework
Hi!

Is there any way to do something like this in a template?

%{ def url = @@{Application.index}}%

I would like to be able to calculate a controller URL and get it into
a variable. I want this for two reasons:

If I need to construct an url and pass that url as parameter in other
URL. For example: http://....?redirect_to=/other/url
The main problem is that /other/url needs to be url-escaped

The other reason is that I have figured out that there is an error in
how URLs are reversed in templates. If you use:

<a href="@@{SomeController.someMethod(param1, param2)}">clic</a>

This is wrong because it can generate something like:

<a href="http://..../someMethod?param1=value1&param2=value2">clic</a>

Why is this wrong? Because the ampresand is not escaped to &amp;
If it could be possible to do this

%{ def url = @@{SomeController.someMethod(param1, param2)} }%

I could make

<a href="${url.escapeHtml()}">clic</a>

And I could make url-scaping if I need to pass an url as parameter
into other URL.

I know that I can use Router.reverse in a template. But is there any
way to do router-reverse and put the value into a variable in an
easier way?

The problem with @@{} and html-escaping is important. However if you
are sending a text/plain mail message you don't want to url-escape. So
I don't know how this could be fixed easily.

So on balance:
+ How can I build an URL in a template and put it into an URL.
+ @@{} and @{} should escape html characters on HTML templates

Thanks!

Guillaume Bort

unread,
Jan 8, 2010, 11:22:53 AM1/8/10
to play-fr...@googlegroups.com
Hi alberto,

Internally, the template translate

@{Application.index()} to something like ${actionBridge.Application.index()}

So I guess that:

%{ def url = actionBridge.Application.index() }%

Should do tricks. But we could also add some helper methods to the
ActionDefinition class, since the actionBridge.Application.index()
call return in fact an ActionDefinition instance.

So we should be able to write things like:

@{Application.index().urlEncode()}

You should report these bugs.

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>
>
>

Alberto Gimeno

unread,
Jan 18, 2010, 12:47:21 PM1/18/10
to play-fr...@googlegroups.com
Thanks Guillaume. Sorry for the late reply.

The actionBridge 'trick' is enough for me at the moment. The only
problem is that if I use:

<a href="${actionBridge.Application.index('foo', 'bar') }">click</a>

I can get something like: <a href="/?b=bar&a=foo">click</a>

Which is wrong becuse the ampersand is not escaped to &amp; If I put

<a href="${actionBridge.Application.index('foo',
'bar').url.escapeHtml() }">click</a>

I get this: <a href="/?b=bar&amp;a=foo">click</a>

Which is OK, but is too verbose.

I'm going to submit a bug report.

Guillaume Bort

unread,
Jan 18, 2010, 1:10:45 PM1/18/10
to play-fr...@googlegroups.com
I think that:

<a href="@{Application.index('foo', 'bar').escape()}">click</a>

should work.

Reply all
Reply to author
Forward
0 new messages