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;
20 return $::request->presenter;
26 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
28 # Set defaults for all available options.
33 $options->{$_} //= $defaults{$_} for keys %defaults;
34 $options->{type} = lc $options->{type};
36 # Check supplied options for validity.
37 foreach (keys %{ $options }) {
38 croak "Unsupported option: $_" unless $defaults{$_};
41 # Only certain types are supported.
42 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json)$/;
44 # The "template" argument must be a string or a reference to one.
45 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
46 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
48 # Look for the file given by $template if $template is not a reference.
51 $source = "templates/webpages/${template}." . $options->{type};
52 croak "Template file ${source} not found" unless -f $source;
54 } elsif (ref($template) eq 'SCALAR') {
55 # Normal scalar reference: hand over to Template
59 # Instance of SL::Presenter::EscapedText. Get reference to its content.
60 $source = \$template->{text};
63 # If no processing is requested then return the content.
64 if (!$options->{process}) {
65 # If $template is a reference then don't try to read a file.
66 my $ref = ref $template;
67 return $template if $ref eq 'SL::Presenter::EscapedText';
68 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if $ref eq 'SCALAR';
70 # Otherwise return the file's content.
71 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
72 my $content = do { local $/ = ''; <$file> };
75 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
78 # Processing was requested. Set up all variables.
79 my %params = ( %locals,
81 FLASH => $::form->{FLASH},
83 INSTANCE_CONF => $::instance_conf,
85 LXCONFIG => \%::lx_office_conf,
86 LXDEBUG => $::lxdebug,
87 MYCONFIG => \%::myconfig,
92 my $parser = $self->get_template;
93 $parser->process($source, \%params, \$output) || croak $parser->error;
95 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
101 $self->{template} ||=
102 Template->new({ INTERPOLATE => 0,
106 PLUGIN_BASE => 'SL::Template::Plugin',
107 INCLUDE_PATH => '.:templates/webpages',
108 COMPILE_EXT => '.tcc',
109 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
110 ERROR => 'templates/webpages/generic/exception.html',
113 return $self->{template};
117 my ($self, $text) = @_;
119 return SL::Presenter::EscapedText->new(text => $text);
123 my ($self, $text) = @_;
125 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
129 my ($self, $text) = @_;
131 $text =~ s|\\|\\\\|g;
132 $text =~ s|\"|\\\"|g;
135 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
144 SL::Presenter - presentation layer class
149 my $presenter = SL::Presenter->get;
151 # Lower-level template parsing:
152 my $html = $presenter->render(
153 'presenter/dir/template.html',
157 # Higher-level rendering of certain objects:
158 use SL::DB::Customer;
160 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
162 # Render a list of links to sales/purchase records:
165 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
166 my $records = $quotation->linked_records(direction => 'to');
167 my $html = $presenter->grouped_record_list($records);
169 =head1 CLASS FUNCTIONS
175 Returns the global presenter object and creates it if it doesn't exist
180 =head1 INSTANCE FUNCTIONS
184 =item C<render $template, [ $options, ] %locals>
186 Renders the template C<$template>. Provides other variables than
187 C<Form::parse_html_template> does.
189 C<$options>, if present, must be a hash reference. All remaining
190 parameters are slurped into C<%locals>.
192 This is the backend function that L<SL::Controller::Base/render>
193 calls. The big difference is that the presenter's L<render> function
194 always returns the input and never sends anything to the browser while
195 the controller's function usually sends the result to the
196 controller. Therefore the presenter's L<render> function does not use
197 all of the parameters for controlling the output that the controller's
200 What is rendered and how C<$template> is interpreted is determined
201 both by C<$template>'s reference type and by the supplied options.
203 If C<$template> is a normal scalar (not a reference) then it is meant
204 to be a template file name relative to the C<templates/webpages>
205 directory. The file name to use is determined by the C<type> option.
207 If C<$template> is a reference to a scalar then the referenced
208 scalar's content is used as the content to process. The C<type> option
209 is not considered in this case.
211 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
212 or a reference to such an instance. Both of these cases are handled
213 the same way as if C<$template> were a reference to a scalar: its
214 content is processed, and C<type> is not considered.
216 Other reference types, unknown options and unknown arguments to the
217 C<type> option cause the function to L<croak>.
219 The following options are available:
225 The template type. Can be C<html> (the default), C<js> for JavaScript
226 or C<json> for JSON content. Affects only the extension that's added
227 to the file name given with a non-reference C<$template> argument.
231 If trueish (which is also the default) it causes the template/content
232 to be processed by the Template toolkit. Otherwise the
233 template/content is returned as-is.
237 If template processing is requested then the template has access to
238 the following variables:
242 =item * C<AUTH> -- C<$::auth>
244 =item * C<FORM> -- C<$::form>
246 =item * C<LOCALE> -- C<$::locale>
248 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
249 with the same name they appear in the file (first level is the
250 section, second the actual variable, e.g. C<system.dbcharset>,
251 C<features.webdav> etc)
253 =item * C<LXDEBUG> -- C<$::lxdebug>
255 =item * C<MYCONFIG> -- C<%::myconfig>
257 =item * C<SELF> -- the controller instance
259 =item * All items from C<%locals>
263 The function will always return the output and never send anything to
266 Example: Render a HTML template with a certain title and a few locals
268 $presenter->render('todo/list',
269 title => 'List TODO items',
270 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
272 Example: Render a string and return its content for further processing
273 by the calling function.
275 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
277 Example: Return the content of a JSON template file without processing
280 my $template_content = $presenter->render(
282 { type => 'json', process => 0 }
285 =item C<escape $text>
287 Returns an HTML-escaped version of C<$text>. Instead of a string an
288 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
291 It is safe to call C<escape> on an instance of
292 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
295 =item C<escaped_text $text>
297 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
298 assumed to be a string that has already been HTML-escaped.
300 It is safe to call C<escaped_text> on an instance of
301 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
304 =item C<escape_js $text>
306 Returns a JavaScript-escaped version of C<$text>. Instead of a string
307 an instance of the thin proxy-object L<SL::Presenter::EscapedText> is
310 It is safe to call C<escape> on an instance of
311 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
314 =item C<get_template>
316 Returns the global instance of L<Template> and creates it if it
317 doesn't exist already.
323 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>