Revision 33b5bec6
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/Controller/Base.pm | ||
---|---|---|
37 | 37 |
my $template = shift; |
38 | 38 |
my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_); |
39 | 39 |
|
40 |
$options->{type} = lc($options->{type} || 'html'); |
|
41 |
$options->{no_layout} = 1 if $options->{type} eq 'js'; |
|
42 |
|
|
40 | 43 |
my $source; |
41 | 44 |
if ($options->{inline}) { |
42 | 45 |
$source = \$template; |
43 | 46 |
|
44 | 47 |
} else { |
45 |
$source = "templates/webpages/${template}.html";
|
|
48 |
$source = "templates/webpages/${template}." . $options->{type};
|
|
46 | 49 |
croak "Template file ${source} not found" unless -f $source; |
47 | 50 |
} |
48 | 51 |
|
49 |
if (!$options->{partial} && !$options->{inline}) { |
|
50 |
$::form->{title} = $locals{title} if $locals{title}; |
|
51 |
$::form->header; |
|
52 |
if (!$options->{partial} && !$options->{inline} && !$::form->{header}) { |
|
53 |
if ($options->{no_layout}) { |
|
54 |
$::form->{header} = 1; |
|
55 |
my $content_type = $options->{type} eq 'js' ? 'text/javascript' : 'text/html'; |
|
56 |
|
|
57 |
print $::form->create_http_response(content_type => $content_type, |
|
58 |
charset => $::dbcharset || Common::DEFAULT_CHARSET); |
|
59 |
|
|
60 |
} else { |
|
61 |
$::form->{title} = $locals{title} if $locals{title}; |
|
62 |
$::form->header; |
|
63 |
} |
|
52 | 64 |
} |
53 | 65 |
|
54 | 66 |
my %params = ( %locals, |
... | ... | |
72 | 84 |
my $parser = $self->_template_obj; |
73 | 85 |
$parser->process($source, \%params, \$output) || croak $parser->error; |
74 | 86 |
|
75 |
print $output unless $options->{inline}; |
|
87 |
print $output unless $options->{inline} || $options->{no_output};
|
|
76 | 88 |
|
77 | 89 |
return $output; |
78 | 90 |
} |
... | ... | |
287 | 299 |
parameters are slurped into C<%locals>. |
288 | 300 |
|
289 | 301 |
What is rendered and how C<$template> is interpreted is determined by |
290 |
C<< $options->{inline} >> and C<< $options->{partial} >>.
|
|
302 |
the options I<type>, I<inline>, I<partial> and I<no_layout>.
|
|
291 | 303 |
|
292 | 304 |
If C<< $options->{inline} >> is trueish then C<$template> is a string |
293 | 305 |
containing the template code to interprete. Additionally the output |
... | ... | |
296 | 308 |
|
297 | 309 |
If C<< $options->{inline} >> is falsish then C<$template> is |
298 | 310 |
interpreted as the name of a template file. It is prefixed with |
299 |
"templates/webpages/" and postfixed with ".html". An exception will be |
|
300 |
thrown if that file does not exist. |
|
311 |
"templates/webpages/" and postfixed with a file extension based on |
|
312 |
C<< $options->{type} >>. C<< $options->{type} >> can be either C<html> |
|
313 |
or C<js> and defaults to C<html>. An exception will be thrown if that |
|
314 |
file does not exist. |
|
301 | 315 |
|
302 | 316 |
If C<< $options->{partial} >> or C<< $options->{inline} >> is trueish |
303 |
then C<< $::form->header >> will not be called. Otherwise |
|
304 |
C<< $::form->{header} >> will be set to C<$locals{header}> (only if |
|
305 |
$locals{header} is trueish) and C<< $::form->header >> will be called |
|
306 |
before the template itself is processed. |
|
317 |
then neither the HTTP response header nor the standard HTML header is |
|
318 |
generated. |
|
319 |
|
|
320 |
Otherwise at least the HTTP response header will be generated based on |
|
321 |
the template type (C<< $options->{type} >>). |
|
322 |
|
|
323 |
If the template type is C<html> then the standard HTML header will be |
|
324 |
output via C<< $::form->header >> with C<< $::form->{title} >> set to |
|
325 |
C<$locals{title}> (the latter only if C<$locals{title}> is |
|
326 |
trueish). Setting C<< $options->{no_layout} >> to trueish will prevent |
|
327 |
this. |
|
307 | 328 |
|
308 | 329 |
The template itself has access to the following variables: |
309 | 330 |
|
... | ... | |
334 | 355 |
|
335 | 356 |
The function will always return the output. |
336 | 357 |
|
358 |
Example: Render a HTML template with a certain title and a few locals |
|
359 |
|
|
360 |
$self->render('todo/list', |
|
361 |
title => 'List TODO items', |
|
362 |
TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted); |
|
363 |
|
|
364 |
Example: Render a string and return its content for further processing |
|
365 |
by the calling function. No header is generated due to C<inline>. |
|
366 |
|
|
367 |
my $content = $self->render('[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]', |
|
368 |
{ type => 'js', inline => 1 }); |
|
369 |
|
|
370 |
Example: Render a JavaScript template and send it to the |
|
371 |
browser. Typical use for actions called via AJAX: |
|
372 |
|
|
373 |
$self->render('todo/single_item', { type => 'js' }, |
|
374 |
item => $employee->most_important_todo_item); |
|
375 |
|
|
337 | 376 |
=item C<url_for $url> |
338 | 377 |
|
339 | 378 |
=item C<url_for $params> |
Auch abrufbar als: Unified diff
render() kann nun auch JavaScript-Templates rendern und dafür passende HTTP-Header erzeugen