5 use parent qw(Rose::Object);
10 use SL::Presenter::CustomerVendor;
11 use SL::Presenter::DeliveryOrder;
12 use SL::Presenter::EscapedText;
13 use SL::Presenter::Invoice;
14 use SL::Presenter::Order;
15 use SL::Presenter::Project;
16 use SL::Presenter::Record;
17 use SL::Presenter::SepaExport;
18 use SL::Presenter::Text;
19 use SL::Presenter::Tag;
22 return $::request->presenter;
28 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
30 # Set defaults for all available options.
35 $options->{$_} //= $defaults{$_} for keys %defaults;
36 $options->{type} = lc $options->{type};
38 # Check supplied options for validity.
39 foreach (keys %{ $options }) {
40 croak "Unsupported option: $_" unless $defaults{$_};
43 # Only certain types are supported.
44 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
46 # The "template" argument must be a string or a reference to one.
47 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
48 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
50 # Look for the file given by $template if $template is not a reference.
53 my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
54 $source = "templates/webpages/${template}.${ext}";
55 croak "Template file ${source} not found" unless -f $source;
57 } elsif (ref($template) eq 'SCALAR') {
58 # Normal scalar reference: hand over to Template
62 # Instance of SL::Presenter::EscapedText. Get reference to its content.
63 $source = \$template->{text};
66 # If no processing is requested then return the content.
67 if (!$options->{process}) {
68 # If $template is a reference then don't try to read a file.
69 my $ref = ref $template;
70 return $template if $ref eq 'SL::Presenter::EscapedText';
71 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
73 # Otherwise return the file's content.
74 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
75 my $content = do { local $/ = ''; <$file> };
78 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
81 # Processing was requested. Set up all variables.
82 my %params = ( %locals,
84 FLASH => $::form->{FLASH},
86 INSTANCE_CONF => $::instance_conf,
88 LXCONFIG => \%::lx_office_conf,
89 LXDEBUG => $::lxdebug,
90 MYCONFIG => \%::myconfig,
95 my $parser = $self->get_template;
96 $parser->process($source, \%params, \$output) || croak $parser->error;
98 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
104 $self->{template} ||=
105 Template->new({ INTERPOLATE => 0,
109 PLUGIN_BASE => 'SL::Template::Plugin',
110 INCLUDE_PATH => '.:templates/webpages',
111 COMPILE_EXT => '.tcc',
112 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
113 ERROR => 'templates/webpages/generic/exception.html',
116 return $self->{template};
120 my ($self, $text) = @_;
122 return SL::Presenter::EscapedText->new(text => $text);
126 my ($self, $text) = @_;
128 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
132 my ($self, $text) = @_;
134 $text =~ s|\\|\\\\|g;
135 $text =~ s|\"|\\\"|g;
138 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
147 SL::Presenter - presentation layer class
152 my $presenter = SL::Presenter->get;
154 # Lower-level template parsing:
155 my $html = $presenter->render(
156 'presenter/dir/template.html',
160 # Higher-level rendering of certain objects:
161 use SL::DB::Customer;
163 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
165 # Render a list of links to sales/purchase records:
168 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
169 my $records = $quotation->linked_records(direction => 'to');
170 my $html = $presenter->grouped_record_list($records);
172 =head1 CLASS FUNCTIONS
178 Returns the global presenter object and creates it if it doesn't exist
183 =head1 INSTANCE FUNCTIONS
187 =item C<render $template, [ $options, ] %locals>
189 Renders the template C<$template>. Provides other variables than
190 C<Form::parse_html_template> does.
192 C<$options>, if present, must be a hash reference. All remaining
193 parameters are slurped into C<%locals>.
195 This is the backend function that L<SL::Controller::Base/render>
196 calls. The big difference is that the presenter's L<render> function
197 always returns the input and never sends anything to the browser while
198 the controller's function usually sends the result to the
199 controller. Therefore the presenter's L<render> function does not use
200 all of the parameters for controlling the output that the controller's
203 What is rendered and how C<$template> is interpreted is determined
204 both by C<$template>'s reference type and by the supplied options.
206 If C<$template> is a normal scalar (not a reference) then it is meant
207 to be a template file name relative to the C<templates/webpages>
208 directory. The file name to use is determined by the C<type> option.
210 If C<$template> is a reference to a scalar then the referenced
211 scalar's content is used as the content to process. The C<type> option
212 is not considered in this case.
214 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
215 or a reference to such an instance. Both of these cases are handled
216 the same way as if C<$template> were a reference to a scalar: its
217 content is processed, and C<type> is not considered.
219 Other reference types, unknown options and unknown arguments to the
220 C<type> option cause the function to L<croak>.
222 The following options are available:
228 The template type. Can be C<html> (the default), C<js> for JavaScript,
229 C<json> for JSON and C<text> for plain text content. Affects only the
230 extension that's added to the file name given with a non-reference
231 C<$template> argument.
235 If trueish (which is also the default) it causes the template/content
236 to be processed by the Template toolkit. Otherwise the
237 template/content is returned as-is.
241 If template processing is requested then the template has access to
242 the following variables:
246 =item * C<AUTH> -- C<$::auth>
248 =item * C<FORM> -- C<$::form>
250 =item * C<LOCALE> -- C<$::locale>
252 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
253 with the same name they appear in the file (first level is the
254 section, second the actual variable, e.g. C<system.dbcharset>,
255 C<features.webdav> etc)
257 =item * C<LXDEBUG> -- C<$::lxdebug>
259 =item * C<MYCONFIG> -- C<%::myconfig>
261 =item * C<SELF> -- the controller instance
263 =item * All items from C<%locals>
267 The function will always return the output and never send anything to
270 Example: Render a HTML template with a certain title and a few locals
272 $presenter->render('todo/list',
273 title => 'List TODO items',
274 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
276 Example: Render a string and return its content for further processing
277 by the calling function.
279 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
281 Example: Return the content of a JSON template file without processing
284 my $template_content = $presenter->render(
286 { type => 'json', process => 0 }
289 =item C<escape $text>
291 Returns an HTML-escaped version of C<$text>. Instead of a string an
292 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
295 It is safe to call C<escape> on an instance of
296 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
299 =item C<escaped_text $text>
301 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
302 assumed to be a string that has already been HTML-escaped.
304 It is safe to call C<escaped_text> on an instance of
305 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
308 =item C<escape_js $text>
310 Returns a JavaScript-escaped version of C<$text>. Instead of a string
311 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
314 It is safe to call C<escape> on an instance of
315 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
318 =item C<get_template>
320 Returns the global instance of L<Template> and creates it if it
321 doesn't exist already.
327 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>