Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 7647d46a

Von Moritz Bunkus vor fast 12 Jahren hinzugefügt

  • ID 7647d46acbc2a8253c0afeac5c706c3eb76995d5
  • Vorgänger 42dfa9a7
  • Nachfolger ec27f677

Refactoring: Parameterredesign SL::Controller::Base::render und SL::Presenter::render

Conflicts:
SL/Controller/FinancialControllingReport.pm
SL/Controller/ProjectType.pm

Unterschiede anzeigen:

SL/Presenter.pm
25 25
  my $template           = shift;
26 26
  my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
27 27

  
28
  $options->{type}       = lc($options->{type} || 'html');
28
  # Set defaults for all available options.
29
  my %defaults = (
30
    type       => 'html',
31
    process    => 1,
32
  );
33
  $options->{$_} //= $defaults{$_} for keys %defaults;
34
  $options->{type} = lc $options->{type};
29 35

  
30
  my $source;
31
  if ($options->{inline}) {
32
    $source = \$template;
36
  # Check supplied options for validity.
37
  foreach (keys %{ $options }) {
38
    croak "Unsupported option: $_" unless $defaults{$_};
39
  }
33 40

  
34
  } else {
41
  # Only certain types are supported.
42
  croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/;
43

  
44
  # The "template" argument must be a string or a reference to one.
45
  croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
46

  
47
  # Look for the file given by $template if $template is not a reference.
48
  my $source;
49
  if (!ref $template) {
35 50
    $source = "templates/webpages/${template}." . $options->{type};
36 51
    croak "Template file ${source} not found" unless -f $source;
52

  
53
  } elsif (ref($template) eq 'SCALAR') {
54
    # Normal scalar reference: hand over to Template
55
    $source = $template;
56

  
57
  } else {
58
    # Instance of SL::Presenter::EscapedText. Get reference to its content.
59
    $source = \$template->{text};
60
  }
61

  
62
  # If no processing is requested then return the content.
63
  if (!$options->{process}) {
64
    # If $template is a reference then don't try to read a file.
65
    return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if ref $template;
66

  
67
    # Otherwise return the file's content.
68
    my $file    = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
69
    my $content = do { local $/ = ''; <$file> };
70
    $file->close;
71

  
72
    return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
37 73
  }
38 74

  
75
  # Processing was requested. Set up all variables.
39 76
  my %params = ( %locals,
40 77
                 AUTH          => $::auth,
41 78
                 FLASH         => $::form->{FLASH},
......
147 184
all of the parameters for controlling the output that the controller's
148 185
function does.
149 186

  
150
What is rendered and how C<$template> is interpreted is determined by
151
the options I<type> and I<inline>.
187
What is rendered and how C<$template> is interpreted is determined
188
both by C<$template>'s reference type and by the supplied options.
189

  
190
If C<$template> is a normal scalar (not a reference) then it is meant
191
to be a template file name relative to the C<templates/webpages>
192
directory. The file name to use is determined by the C<type> option.
152 193

  
153
If C<< $options->{inline} >> is trueish then C<$template> is a string
154
containing the template code to interprete.
194
If C<$template> is a reference to a scalar then the referenced
195
scalar's content is used as the content to process. The C<type> option
196
is not considered in this case.
155 197

  
156
If C<< $options->{inline} >> is falsish then C<$template> is
157
interpreted as the name of a template file. It is prefixed with
158
"templates/webpages/" and postfixed with a file extension based on
159
C<< $options->{type} >>. C<< $options->{type} >> can be either C<html>
160
or C<js> and defaults to C<html>. An exception will be thrown if that
161
file does not exist.
198
Other reference types, unknown options and unknown arguments to the
199
C<type> option cause the function to L<croak>.
162 200

  
163
The template itself has access to the following variables:
201
The following options are available:
202

  
203
=over 2
204

  
205
=item C<type>
206

  
207
The template type. Can be C<html> (the default), C<js> for JavaScript
208
or C<json> for JSON content. Affects only the extension that's added
209
to the file name given with a non-reference C<$template> argument.
210

  
211
=item C<process>
212

  
213
If trueish (which is also the default) it causes the template/content
214
to be processed by the Template toolkit. Otherwise the
215
template/content is returned as-is.
216

  
217
=back
218

  
219
If template processing is requested then the template has access to
220
the following variables:
164 221

  
165 222
=over 2
166 223

  
......
197 254
Example: Render a string and return its content for further processing
198 255
by the calling function.
199 256

  
200
  my $content = $presenter->render(
201
    '[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]',
202
    { type => 'js', inline => 1 }
257
  my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
258

  
259
Example: Return the content of a JSON template file without processing
260
it at all:
261

  
262
  my $template_content = $presenter->render(
263
    'customer/contact',
264
    { type => 'json', process => 0 }
203 265
  );
204 266

  
205 267
=item C<escape $text>

Auch abrufbar als: Unified diff