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::Text;
18 use SL::Presenter::Tag;
21 return $::request->presenter;
27 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
29 # Set defaults for all available options.
34 $options->{$_} //= $defaults{$_} for keys %defaults;
35 $options->{type} = lc $options->{type};
37 # Check supplied options for validity.
38 foreach (keys %{ $options }) {
39 croak "Unsupported option: $_" unless $defaults{$_};
42 # Only certain types are supported.
43 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
45 # The "template" argument must be a string or a reference to one.
46 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
47 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
49 # Look for the file given by $template if $template is not a reference.
52 my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
53 $source = "templates/webpages/${template}.${ext}";
54 croak "Template file ${source} not found" unless -f $source;
56 } elsif (ref($template) eq 'SCALAR') {
57 # Normal scalar reference: hand over to Template
61 # Instance of SL::Presenter::EscapedText. Get reference to its content.
62 $source = \$template->{text};
65 # If no processing is requested then return the content.
66 if (!$options->{process}) {
67 # If $template is a reference then don't try to read a file.
68 my $ref = ref $template;
69 return $template if $ref eq 'SL::Presenter::EscapedText';
70 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
72 # Otherwise return the file's content.
73 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
74 my $content = do { local $/ = ''; <$file> };
77 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
80 # Processing was requested. Set up all variables.
81 my %params = ( %locals,
83 FLASH => $::form->{FLASH},
85 INSTANCE_CONF => $::instance_conf,
87 LXCONFIG => \%::lx_office_conf,
88 LXDEBUG => $::lxdebug,
89 MYCONFIG => \%::myconfig,
94 my $parser = $self->get_template;
95 $parser->process($source, \%params, \$output) || croak $parser->error;
97 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
103 $self->{template} ||=
104 Template->new({ INTERPOLATE => 0,
108 PLUGIN_BASE => 'SL::Template::Plugin',
109 INCLUDE_PATH => '.:templates/webpages',
110 COMPILE_EXT => '.tcc',
111 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
112 ERROR => 'templates/webpages/generic/exception.html',
115 return $self->{template};
119 my ($self, $text) = @_;
121 return SL::Presenter::EscapedText->new(text => $text);
125 my ($self, $text) = @_;
127 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
131 my ($self, $text) = @_;
133 $text =~ s|\\|\\\\|g;
134 $text =~ s|\"|\\\"|g;
137 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
146 SL::Presenter - presentation layer class
151 my $presenter = SL::Presenter->get;
153 # Lower-level template parsing:
154 my $html = $presenter->render(
155 'presenter/dir/template.html',
159 # Higher-level rendering of certain objects:
160 use SL::DB::Customer;
162 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
164 # Render a list of links to sales/purchase records:
167 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
168 my $records = $quotation->linked_records(direction => 'to');
169 my $html = $presenter->grouped_record_list($records);
171 =head1 CLASS FUNCTIONS
177 Returns the global presenter object and creates it if it doesn't exist
182 =head1 INSTANCE FUNCTIONS
186 =item C<render $template, [ $options, ] %locals>
188 Renders the template C<$template>. Provides other variables than
189 C<Form::parse_html_template> does.
191 C<$options>, if present, must be a hash reference. All remaining
192 parameters are slurped into C<%locals>.
194 This is the backend function that L<SL::Controller::Base/render>
195 calls. The big difference is that the presenter's L<render> function
196 always returns the input and never sends anything to the browser while
197 the controller's function usually sends the result to the
198 controller. Therefore the presenter's L<render> function does not use
199 all of the parameters for controlling the output that the controller's
202 What is rendered and how C<$template> is interpreted is determined
203 both by C<$template>'s reference type and by the supplied options.
205 If C<$template> is a normal scalar (not a reference) then it is meant
206 to be a template file name relative to the C<templates/webpages>
207 directory. The file name to use is determined by the C<type> option.
209 If C<$template> is a reference to a scalar then the referenced
210 scalar's content is used as the content to process. The C<type> option
211 is not considered in this case.
213 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
214 or a reference to such an instance. Both of these cases are handled
215 the same way as if C<$template> were a reference to a scalar: its
216 content is processed, and C<type> is not considered.
218 Other reference types, unknown options and unknown arguments to the
219 C<type> option cause the function to L<croak>.
221 The following options are available:
227 The template type. Can be C<html> (the default), C<js> for JavaScript,
228 C<json> for JSON and C<text> for plain text content. Affects only the
229 extension that's added to the file name given with a non-reference
230 C<$template> argument.
234 If trueish (which is also the default) it causes the template/content
235 to be processed by the Template toolkit. Otherwise the
236 template/content is returned as-is.
240 If template processing is requested then the template has access to
241 the following variables:
245 =item * C<AUTH> -- C<$::auth>
247 =item * C<FORM> -- C<$::form>
249 =item * C<LOCALE> -- C<$::locale>
251 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
252 with the same name they appear in the file (first level is the
253 section, second the actual variable, e.g. C<system.dbcharset>,
254 C<features.webdav> etc)
256 =item * C<LXDEBUG> -- C<$::lxdebug>
258 =item * C<MYCONFIG> -- C<%::myconfig>
260 =item * C<SELF> -- the controller instance
262 =item * All items from C<%locals>
266 The function will always return the output and never send anything to
269 Example: Render a HTML template with a certain title and a few locals
271 $presenter->render('todo/list',
272 title => 'List TODO items',
273 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
275 Example: Render a string and return its content for further processing
276 by the calling function.
278 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
280 Example: Return the content of a JSON template file without processing
283 my $template_content = $presenter->render(
285 { type => 'json', process => 0 }
288 =item C<escape $text>
290 Returns an HTML-escaped version of C<$text>. Instead of a string an
291 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
294 It is safe to call C<escape> on an instance of
295 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
298 =item C<escaped_text $text>
300 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
301 assumed to be a string that has already been HTML-escaped.
303 It is safe to call C<escaped_text> on an instance of
304 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
307 =item C<escape_js $text>
309 Returns a JavaScript-escaped version of C<$text>. Instead of a string
310 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
313 It is safe to call C<escape> on an instance of
314 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
317 =item C<get_template>
319 Returns the global instance of L<Template> and creates it if it
320 doesn't exist already.
326 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>