[Catalyst] Re: Does uri_for() URL-escape arguments correctly ?

Aristotle Pagaltzis pagaltzis at gmx.de
Thu Dec 6 06:36:15 GMT 2012


I have (lots of stuff like) the following in MyApp::View::TT:

    sub uri_for_action {
        my $self = shift;
        my $c = shift;
        return $c->uri_for_action( @_ );
    }

    __PACKAGE__->config( expose_methods => [ qw(
        uri_for_action
        ...
    ) ] );

This lets me write just

    <a href="[% uri_for_action('object', [ file ]) %]">
        <img src="[% uri_for_action('thumbnail', [ file ]) %]" alt="[% video | html %]" />
    </a>

Note the absence of `c`. That absence was the rationale for this – it
makes it much easier to rig up a test environment for the templates if
they do not refer to the context directly.

But in your case, it would also give you a central place where you could
stick the pre-escaping of %.

Something along the lines of

    sub uri_for_action {
        my $self = shift;
        my $c = shift;
        my $action = shift;
        for ( @_ ) {
            my $r = ref $_;
            s/%/%25/g for
                'ARRAY' eq $r ? @$_ :
                'HASH'  eq $r ? values %$_ :
                ( not $r )    ? $_ :
                ();
        }
        return $c->uri_for_action( $action, @_ );
    }

(Note the code is entirely untested and may be buggy not just
in implementation but also conceptually – it is meant only for
illustration.)

See `expose_methods` in the Catalyst::View::TT docs.

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>



More information about the Catalyst mailing list