SL::Presenter -- die neue Präsentationsschicht
[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 '' unless $presenter->can($method);
33
34   splice @args, -1, 1, %{ $args[-1] } if @args && (ref($args[-1]) eq 'HASH');
35
36   $presenter->$method(@args);
37 }
38
39 1;
40
41 __END__
42
43 =pod
44
45 =encoding utf8
46
47 =head1 NAME
48
49 SL::Template::Plugin::P - Template plugin for the presentation layer
50
51 =head1 SYNOPSIS
52
53   [% USE P %]
54
55   Customer: [% P.customer(customer) %]
56
57   Linked records:
58   [% P.grouped_record_list(RECORDS) %]
59
60 =head1 FUNCTIONS
61
62 =over 4
63
64 =item C<AUTOLOAD>
65
66 All unknown functions called on C<P> are forwarded to functions with
67 the same name in the global presenter object.
68
69 The presenter's functions use hashes for named-argument
70 passing. Unfortunately L<Template> groups named arguments into hash
71 references. This makes mixing intentional hash references and named
72 arguments a bit hairy. For example, the following calls from a
73 template are undistinguishable for a plugin:
74
75   [% P.some_func({ arg1 => 42, arg2 => 'Charlie' }) %]
76   [% P.some_func(arg1 => 42, arg2 => 'Charlie') %]
77   [% P.some_func(arg1=42, arg2='Charlie') %]
78   [% P.some_func(arg1=42, arg2='Charlie') %]
79
80 C<AUTOLOAD> tries to be clever and unpacks a hash reference into a
81 normal hash if and only if it is the very last parameter to the
82 function call.
83
84 Returns the result of the corresponding function in the presenter.
85
86 =item C<escape $text>
87
88 Returns an HTML-escaped version of C<$text>. Instead of a string an
89 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
90 returned.
91
92 It is safe to call C<escape> on an instance of
93 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
94 be returned).
95
96 =back
97
98 =head1 BUGS
99
100 Nothing here yet.
101
102 =head1 AUTHOR
103
104 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
105
106 =cut