aboutsummaryrefslogtreecommitdiffstats
path: root/perllib/FixMyStreet/Template/Context.pm
blob: de3212095f81805a2faf427840d91a28bed9b0e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package FixMyStreet::Template::Context;

use strict;
use warnings;
use base qw(Template::Context);

sub filter {
    my $self = shift;
    my ($name, $args, $alias) = @_;

    # If we're passing through the safe filter, then unwrap
    # from a Template::HTML::Variable if we are one.
    if ( $name eq 'safe' ) {
        return sub {
            my $value = shift;
            return $value->plain if UNIVERSAL::isa($value, 'FixMyStreet::Template::Variable');
            return $value;
        };
    }

    my $filter = $self->SUPER::filter(@_);

    # If we are already going to auto-encode, we don't want to do it again.
    # This makes the html filter a no-op on auto-encoded variables.
    if ( $name eq 'html' ) {
        return sub {
            my $value = shift;
            return $value if UNIVERSAL::isa($value, 'FixMyStreet::Template::Variable');
            return $filter->($value);
        };
    }

    return sub {
        my $value = shift;

        if ( UNIVERSAL::isa($value, 'FixMyStreet::Template::Variable') ) {
            my $result = $filter->($value->plain);
            return $result if UNIVERSAL::isa($result, 'FixMyStreet::Template::SafeString');
            return ref($value)->new($result);
        }

        return $filter->($value);
    };
}

1;
__END__

=head1 NAME

FixMyStreet::Template::Context - Similar to Template::HTML::Context but use
'safe' rather than 'none' to be clear, also prevents html filter double-encoding,
and doesn't rewrap a FixMyStreet::Template::SafeString.

=head1 AUTHORS

Martyn Smith, E<lt>msmith@cpan.orgE<gt>

Matthew Somerville, E<lt>matthew@mysociety.orgE<gt>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut