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