Links between Mason components?by tye (Cardinal)
|on May 17, 2012 at 17:15 UTC||Need Help??|
tye has asked for the
wisdom of the Perl Monks concerning the following question:
Have I just not found it? It seems a pretty basic, fundamental, and important feature. And I have not yet found any built-in support in HTML::Mason for constructing a link (as in <a href=...) from one Mason component to another where that link includes arguments.
That is, other than a quite awkward construction like:
Having to remember to include "|u" for each parameter makes that nearly unacceptable in my book (after seeing way too many bugs from lack of URL escaping that pass unnoticed for a long time and then turn into a crisis, even a security problem). It also gets quite tedious (and error-prone and hard to read) when producing a table full of similar links.
But to see how awkward that really can be, imagine what I find to be a common case: Having a hash of arguments that you want to include in the link. Am I really supposed to roll my own URL constructor for such an obvious case?
Although that first line is a reasonable interface, the implementation, of course, doesn't actually work, producing:
Is there a better (and actually correct) way to write such in Mason?
Too bad defining a "removing newlines and adjacent whitespace" Mason filter (call it "|w") doesn't allow me to address this problem as simply as:
(You can use "|w" inside of <% ... %> but not inside of <& ... &>.)
So, (at least for now) I resign myself to looking outside of Mason for a solution.
My first stop was CGI because I already know that CGI.pm knows how to construct a URL with parameters. I know it even allows me to choose to use ';' to separate parameters instead of the old, ugly '&'. Unfortunately, I end up disappointed to find that CGI.pm only knows how to construct URLs to the current page.
Clearly, URI knows how to construct URLs. Of course it does. Sadly, it doesn't appear to know anything about CGI parameters in a URL.
So, I roll these pieces together and get:
And try to use that in my Mason:
Note the gyrations to prevent .link from including newlines.
Okay, that is quite a bit uglier than I had hoped for. But it actually works.
Oh, except, as we already mentioned, you can't use something like "|sq" with <& ... &>.
I could define .sq that escapes the string passed to it. Ooh, I just found this syntax:
That actually addresses (if in a manner still uglier than I had hoped) some of the questions I had when I started writing this.
What other features am I missing? How can I do this better?