44eeb436df35a5ca6997e5b89209f0e470b3006b
[kivitendo-erp.git] / SL / Template / Plugin / P.pm
1 package SL::Template::Plugin::P;
2
3 use base qw( Template::Plugin );
4
5 use SL::Presenter;
6 use SL::Presenter::EscapedText;
7
8 use strict;
9
10 sub new {
11   my ($class, $context, @args) = @_;
12
13   return bless {
14     CONTEXT => $context,
15   }, $class;
16 }
17
18 sub escape {
19   my ($self, $string) = @_;
20   return SL::Presenter::EscapedText->new(text => $string);
21 }
22
23 sub AUTOLOAD {
24   our $AUTOLOAD;
25
26   my ($self, @args) = @_;
27
28   my $presenter     = SL::Presenter->get;
29   my $method        =  $AUTOLOAD;
30   $method           =~ s/.*:://;
31
32   return '' if $method eq 'DESTROY';
33
34   if (!$presenter->can($method)) {
35     $::lxdebug->message(LXDebug::WARN(), "SL::Presenter has no method named '$method'!");
36     return '';
37   }
38
39   splice @args, -1, 1, %{ $args[-1] } if @args && (ref($args[-1]) eq 'HASH');
40
41   $presenter->$method(@args);
42 }
43
44 1;
45
46 __END__
47
48 =pod
49
50 =encoding utf8
51
52 =head1 NAME
53
54 SL::Template::Plugin::P - Template plugin for the presentation layer
55
56 =head1 SYNOPSIS
57
58   [% USE P %]
59
60   Customer: [% P.customer(customer) %]
61
62   Linked records:
63   [% P.grouped_record_list(RECORDS) %]
64
65 =head1 FUNCTIONS
66
67 =over 4
68
69 =item C<AUTOLOAD>
70
71 All unknown functions called on C<P> are forwarded to functions with
72 the same name in the global presenter object.
73
74 The presenter's functions use hashes for named-argument
75 passing. Unfortunately L<Template> groups named arguments into hash
76 references. This makes mixing intentional hash references and named
77 arguments a bit hairy. For example, the following calls from a
78 template are undistinguishable for a plugin:
79
80   [% P.some_func({ arg1 => 42, arg2 => 'Charlie' }) %]
81   [% P.some_func(arg1 => 42, arg2 => 'Charlie') %]
82   [% P.some_func(arg1=42, arg2='Charlie') %]
83   [% P.some_func(arg1=42, arg2='Charlie') %]
84
85 C<AUTOLOAD> tries to be clever and unpacks a hash reference into a
86 normal hash if and only if it is the very last parameter to the
87 function call.
88
89 Returns the result of the corresponding function in the presenter.
90
91 =item C<escape $text>
92
93 Returns an HTML-escaped version of C<$text>. Instead of a string an
94 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
95 returned.
96
97 It is safe to call C<escape> on an instance of
98 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
99 be returned).
100
101 =back
102
103 =head1 BUGS
104
105 Nothing here yet.
106
107 =head1 AUTHOR
108
109 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
110
111 =cut