Revision 7647d46a
Von Moritz Bunkus vor fast 12 Jahren hinzugefügt
SL/Controller/Base.pm | ||
---|---|---|
60 | 60 |
my $template = shift; |
61 | 61 |
my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_); |
62 | 62 |
|
63 |
$options->{type} = lc($options->{type} || 'html'); |
|
64 |
$options->{no_layout} = 1 if $options->{type} eq 'js'; |
|
63 |
# Set defaults for all available options. |
|
64 |
my %defaults = ( |
|
65 |
type => 'html', |
|
66 |
output => 1, |
|
67 |
header => 1, |
|
68 |
layout => 1, |
|
69 |
process => 1, |
|
70 |
); |
|
71 |
$options->{$_} //= $defaults{$_} for keys %defaults; |
|
72 |
$options->{type} = lc $options->{type}; |
|
73 |
|
|
74 |
# Check supplied options for validity. |
|
75 |
foreach (keys %{ $options }) { |
|
76 |
croak "Unsupported option: $_" unless $defaults{$_}; |
|
77 |
} |
|
78 |
|
|
79 |
# Only certain types are supported. |
|
80 |
croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/; |
|
81 |
|
|
82 |
# The "template" argument must be a string or a reference to one. |
|
83 |
croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/); |
|
84 |
|
|
85 |
# If all output is turned off then don't output the header either. |
|
86 |
if (!$options->{output}) { |
|
87 |
$options->{header} = 0; |
|
88 |
$options->{layout} = 0; |
|
89 |
|
|
90 |
} else { |
|
91 |
# Layout only makes sense if we're outputting HTML. |
|
92 |
$options->{layout} = 0 if $options->{type} ne 'html'; |
|
93 |
} |
|
94 |
|
|
95 |
if ($options->{header}) { |
|
96 |
# Output the HTTP response and the layout in case of HTML output. |
|
65 | 97 |
|
66 |
if (!$options->{partial} && !$options->{inline} && !$::form->{header}) { |
|
67 |
if ($options->{no_layout}) { |
|
98 |
if ($options->{layout}) { |
|
99 |
$::form->{title} = $locals{title} if $locals{title}; |
|
100 |
$::form->header; |
|
101 |
|
|
102 |
} else { |
|
103 |
# No layout: just the standard HTTP response. Also notify |
|
104 |
# $::form that the header has already been output so that |
|
105 |
# $::form->header() won't output it again. |
|
68 | 106 |
$::form->{header} = 1; |
69 |
my $content_type = $options->{type} eq 'js' ? 'text/javascript' : 'text/html'; |
|
107 |
my $content_type = $options->{type} eq 'html' ? 'text/html' |
|
108 |
: $options->{type} eq 'js' ? 'text/javascript' |
|
109 |
: 'application/json'; |
|
70 | 110 |
|
71 | 111 |
print $::form->create_http_response(content_type => $content_type, |
72 | 112 |
charset => $::lx_office_conf{system}->{dbcharset} || Common::DEFAULT_CHARSET()); |
73 |
|
|
74 |
} else { |
|
75 |
$::form->{title} = $locals{title} if $locals{title}; |
|
76 |
$::form->header(no_menu => $options->{no_menu}); |
|
77 | 113 |
} |
78 | 114 |
} |
79 | 115 |
|
80 |
my $output; |
|
81 |
if ($options->{raw}) { |
|
82 |
$output = $$template; |
|
83 |
} else { |
|
84 |
$output = $self->presenter->render( |
|
85 |
$template, $options, |
|
86 |
%locals, |
|
87 |
SELF => $self, |
|
88 |
); |
|
89 |
} |
|
116 |
# Let the presenter do the rest of the work. |
|
117 |
my $output = $self->presenter->render( |
|
118 |
$template, |
|
119 |
{ type => $options->{type}, process => $options->{process} }, |
|
120 |
%locals, |
|
121 |
SELF => $self, |
|
122 |
); |
|
90 | 123 |
|
91 |
print $output unless $options->{inline} || $options->{no_output}; |
|
124 |
# Print the output if wanted. |
|
125 |
print $output if $options->{output}; |
|
92 | 126 |
|
93 | 127 |
return $output; |
94 | 128 |
} |
... | ... | |
334 | 368 |
C<$options>, if present, must be a hash reference. All remaining |
335 | 369 |
parameters are slurped into C<%locals>. |
336 | 370 |
|
337 |
What is rendered and how C<$template> is interpreted is determined by
|
|
338 |
the options I<type>, I<inline>, I<partial> and I<no_layout>. The
|
|
371 |
What is rendered and how C<$template> is interpreted is determined |
|
372 |
both by C<$template>'s reference type and by the supplied options. The
|
|
339 | 373 |
actual rendering is handled by L<SL::Presenter/render>. |
340 | 374 |
|
341 |
If C<< $options->{inline} >> is trueish then C<$template> is a string |
|
342 |
containing the template code to interprete. Additionally the output |
|
343 |
will not be sent to the browser. Instead it is only returned to the |
|
344 |
caller. |
|
375 |
If C<$template> is a normal scalar (not a reference) then it is meant |
|
376 |
to be a template file name relative to the C<templates/webpages> |
|
377 |
directory. The file name to use is determined by the C<type> option. |
|
378 |
|
|
379 |
If C<$template> is a reference to a scalar then the referenced |
|
380 |
scalar's content is used as the content to process. The C<type> option |
|
381 |
is not considered in this case. |
|
382 |
|
|
383 |
Other reference types, unknown options and unknown arguments to the |
|
384 |
C<type> option cause the function to L<croak>. |
|
385 |
|
|
386 |
The following options are available (defaults: C<type> = 'html', |
|
387 |
C<process> = 1, C<output> = 1, C<header> = 1, C<layout> = 1): |
|
388 |
|
|
389 |
=over 2 |
|
390 |
|
|
391 |
=item C<type> |
|
345 | 392 |
|
346 |
If C<< $options->{raw} >> is trueish, the function will treat the |
|
347 |
input as already parsed, and will not filter the input through |
|
348 |
Template. This also means that L<SL::Presenter/render> is not |
|
349 |
called either. Unlike C<inline>, the input is taken as a reference. |
|
393 |
The template type. Can be C<html> (the default), C<js> for JavaScript |
|
394 |
or C<json> for JSON content. Affects the extension that's added to the |
|
395 |
file name given with a non-reference C<$template> argument, the |
|
396 |
content type HTTP header that is output and whether or not the layout |
|
397 |
will be output as well (see description of C<layout> below). |
|
350 | 398 |
|
351 |
If C<< $options->{inline} >> is falsish then C<$template> is |
|
352 |
interpreted as the name of a template file. It is prefixed with |
|
353 |
"templates/webpages/" and postfixed with a file extension based on |
|
354 |
C<< $options->{type} >>. C<< $options->{type} >> can be either C<html> |
|
355 |
or C<js> and defaults to C<html>. An exception will be thrown if that |
|
356 |
file does not exist. |
|
399 |
=item C<process> |
|
357 | 400 |
|
358 |
If C<< $options->{partial} >> or C<< $options->{inline} >> is trueish
|
|
359 |
then neither the HTTP response header nor the standard HTML header is
|
|
360 |
generated.
|
|
401 |
If trueish (which is also the default) it causes the template/content
|
|
402 |
to be processed by the Template toolkit. Otherwise the
|
|
403 |
template/content is output as-is.
|
|
361 | 404 |
|
362 |
Otherwise at least the HTTP response header will be generated based on |
|
363 |
the template type (C<< $options->{type} >>). |
|
405 |
=item C<output> |
|
364 | 406 |
|
365 |
If the template type is C<html> then the standard HTML header will be |
|
366 |
output via C<< $::form->header >> with C<< $::form->{title} >> set to |
|
367 |
C<$locals{title}> (the latter only if C<$locals{title}> is |
|
368 |
trueish). Setting C<< $options->{no_layout} >> to trueish will prevent |
|
369 |
this. |
|
407 |
If trueish (the default) then the generated output will be sent to the |
|
408 |
browser in addition to being returned. If falsish then the options |
|
409 |
C<header> and C<layout> are set to 0 as well. |
|
410 |
|
|
411 |
=item C<header> |
|
412 |
|
|
413 |
Determines whether or not to output the HTTP response |
|
414 |
headers. Defaults to the same value that C<output> is set to. If set |
|
415 |
to falsish then the layout is not output either. |
|
416 |
|
|
417 |
=item C<layout> |
|
418 |
|
|
419 |
Determines whether or not the basic HTML layout structure should be |
|
420 |
output (HTML header, common JavaScript and stylesheet inclusions, menu |
|
421 |
etc.). Defaults to 0 if C<type> is not C<html> and to the same value |
|
422 |
C<header> is set to otherwise. |
|
423 |
|
|
424 |
=back |
|
370 | 425 |
|
371 | 426 |
The template itself has access to several variables. These are listed |
372 | 427 |
in the documentation to L<SL::Presenter/render>. |
373 | 428 |
|
374 |
Unless C<< $options->{inline} >> is trueish the function will send the |
|
375 |
output to the browser. |
|
376 |
|
|
377 | 429 |
The function will always return the output. |
378 | 430 |
|
379 | 431 |
Example: Render a HTML template with a certain title and a few locals |
... | ... | |
383 | 435 |
TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted); |
384 | 436 |
|
385 | 437 |
Example: Render a string and return its content for further processing |
386 |
by the calling function. No header is generated due to C<inline>.
|
|
438 |
by the calling function. No header is generated due to C<output>.
|
|
387 | 439 |
|
388 |
my $content = $self->render('[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]', |
|
389 |
{ type => 'js', inline => 1 });
|
|
440 |
my $content = $self->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]',
|
|
441 |
{ output => 0 });
|
|
390 | 442 |
|
391 |
Example: Render a JavaScript template and send it to the |
|
443 |
Example: Render a JavaScript template |
|
444 |
"templates/webpages/todo/single_item.js" and send it to the |
|
392 | 445 |
browser. Typical use for actions called via AJAX: |
393 | 446 |
|
394 | 447 |
$self->render('todo/single_item', { type => 'js' }, |
Auch abrufbar als: Unified diff
Refactoring: Parameterredesign SL::Controller::Base::render und SL::Presenter::render
Conflicts:
SL/Controller/FinancialControllingReport.pm
SL/Controller/ProjectType.pm