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;
19 return $::request->presenter;
25 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
27 # Set defaults for all available options.
32 $options->{$_} //= $defaults{$_} for keys %defaults;
33 $options->{type} = lc $options->{type};
35 # Check supplied options for validity.
36 foreach (keys %{ $options }) {
37 croak "Unsupported option: $_" unless $defaults{$_};
40 # Only certain types are supported.
41 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/;
43 # The "template" argument must be a string or a reference to one.
44 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
45 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
47 # Look for the file given by $template if $template is not a reference.
50 $source = "templates/webpages/${template}." . $options->{type};
51 croak "Template file ${source} not found" unless -f $source;
53 } elsif (ref($template) eq 'SCALAR') {
54 # Normal scalar reference: hand over to Template
58 # Instance of SL::Presenter::EscapedText. Get reference to its content.
59 $source = \$template->{text};
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 my $ref = ref $template;
66 return $template if $ref eq 'SL::Presenter::EscapedText';
67 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
69 # Otherwise return the file's content.
70 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
71 my $content = do { local $/ = ''; <$file> };
74 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
77 # Processing was requested. Set up all variables.
78 my %params = ( %locals,
80 FLASH => $::form->{FLASH},
82 INSTANCE_CONF => $::instance_conf,
84 LXCONFIG => \%::lx_office_conf,
85 LXDEBUG => $::lxdebug,
86 MYCONFIG => \%::myconfig,
91 my $parser = $self->get_template;
92 $parser->process($source, \%params, \$output) || croak $parser->error;
94 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
100 $self->{template} ||=
101 Template->new({ INTERPOLATE => 0,
105 PLUGIN_BASE => 'SL::Template::Plugin',
106 INCLUDE_PATH => '.:templates/webpages',
107 COMPILE_EXT => '.tcc',
108 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
109 ERROR => 'templates/webpages/generic/exception.html',
112 return $self->{template};
116 my ($self, $text) = @_;
118 return SL::Presenter::EscapedText->new(text => $text);
122 my ($self, $text) = @_;
124 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
133 SL::Presenter - presentation layer class
138 my $presenter = SL::Presenter->get;
140 # Lower-level template parsing:
141 my $html = $presenter->render(
142 'presenter/dir/template.html',
146 # Higher-level rendering of certain objects:
147 use SL::DB::Customer;
149 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
151 # Render a list of links to sales/purchase records:
154 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
155 my $records = $quotation->linked_records(direction => 'to');
156 my $html = $presenter->grouped_record_list($records);
158 =head1 CLASS FUNCTIONS
164 Returns the global presenter object and creates it if it doesn't exist
169 =head1 INSTANCE FUNCTIONS
173 =item C<render $template, [ $options, ] %locals>
175 Renders the template C<$template>. Provides other variables than
176 C<Form::parse_html_template> does.
178 C<$options>, if present, must be a hash reference. All remaining
179 parameters are slurped into C<%locals>.
181 This is the backend function that L<SL::Controller::Base/render>
182 calls. The big difference is that the presenter's L<render> function
183 always returns the input and never sends anything to the browser while
184 the controller's function usually sends the result to the
185 controller. Therefore the presenter's L<render> function does not use
186 all of the parameters for controlling the output that the controller's
189 What is rendered and how C<$template> is interpreted is determined
190 both by C<$template>'s reference type and by the supplied options.
192 If C<$template> is a normal scalar (not a reference) then it is meant
193 to be a template file name relative to the C<templates/webpages>
194 directory. The file name to use is determined by the C<type> option.
196 If C<$template> is a reference to a scalar then the referenced
197 scalar's content is used as the content to process. The C<type> option
198 is not considered in this case.
200 Other reference types, unknown options and unknown arguments to the
201 C<type> option cause the function to L<croak>.
203 The following options are available:
209 The template type. Can be C<html> (the default), C<js> for JavaScript
210 or C<json> for JSON content. Affects only the extension that's added
211 to the file name given with a non-reference C<$template> argument.
215 If trueish (which is also the default) it causes the template/content
216 to be processed by the Template toolkit. Otherwise the
217 template/content is returned as-is.
221 If template processing is requested then the template has access to
222 the following variables:
226 =item * C<AUTH> -- C<$::auth>
228 =item * C<FORM> -- C<$::form>
230 =item * C<LOCALE> -- C<$::locale>
232 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
233 with the same name they appear in the file (first level is the
234 section, second the actual variable, e.g. C<system.dbcharset>,
235 C<features.webdav> etc)
237 =item * C<LXDEBUG> -- C<$::lxdebug>
239 =item * C<MYCONFIG> -- C<%::myconfig>
241 =item * C<SELF> -- the controller instance
243 =item * All items from C<%locals>
247 The function will always return the output and never send anything to
250 Example: Render a HTML template with a certain title and a few locals
252 $presenter->render('todo/list',
253 title => 'List TODO items',
254 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
256 Example: Render a string and return its content for further processing
257 by the calling function.
259 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
261 Example: Return the content of a JSON template file without processing
264 my $template_content = $presenter->render(
266 { type => 'json', process => 0 }
269 =item C<escape $text>
271 Returns an HTML-escaped version of C<$text>. Instead of a string an
272 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
275 It is safe to call C<escape> on an instance of
276 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
279 =item C<escaped_text $text>
281 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
282 assumed to be a string that has already been HTML-escaped.
284 It is safe to call C<escaped_text> on an instance of
285 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
288 =item C<get_template>
290 Returns the global instance of L<Template> and creates it if it
291 doesn't exist already.
297 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>