test action
[kivitendo-erp.git] / SL / Template / Plugin / HTMLFixes.pm
1 package SL::Template::Plugin::HTMLFixes;
2
3 use Template::Plugin::HTML;
4 use Template::Stash;
5
6 1;
7
8 package Template::Plugin::HTML;
9
10 use strict;
11
12 use Encode;
13
14 # Replacement for Template::Plugin::HTML::url.
15
16 # Strings in kivitendo are stored in Perl's internal encoding but have
17 # to be output as UTF-8. A normal regex replace doesn't do that
18 # creating invalid UTF-8 characters upon URL-unescaping.
19
20 # The only addition is the "Encode::encode()" line.
21 no warnings 'redefine';
22 sub url {
23     my ($self, $text) = @_;
24     return undef unless defined $text;
25     $text =  Encode::encode('utf-8-strict', $text);
26     $text =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
27     return $text;
28 }
29
30 1;
31
32 package Template::Stash;
33
34 # A method for forcing list context. If a method uses 'wantarray' then
35 # calling that method from Template will do strange stuff like chosing
36 # scalar context. The most obvious offender are RDBO relationships.
37
38 # Example of how NOT to test whether or not a customer has contacts:
39 #   [% IF customer.contacts.size %] ...
40 # Instead force list context and then test the size:
41 #   [% IF customer.contacts.as_list.size %] ...
42 $Template::Stash::LIST_OPS->{ as_list } = sub {
43   return ref( $_[0] ) eq 'ARRAY' ? shift : [shift];
44 };
45
46 1;