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 $::request->{presenter} ||= SL::Presenter->new;
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 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 return SL::Presenter::EscapedText->new(text => ${ $template }, is_escaped => 1) if ref $template;
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> };
72 return SL::Presenter::EscapedText->new(text => $content, is_escaped => 1);
75 # Processing was requested. Set up all variables.
76 my %params = ( %locals,
78 FLASH => $::form->{FLASH},
80 INSTANCE_CONF => $::instance_conf,
82 LXCONFIG => \%::lx_office_conf,
83 LXDEBUG => $::lxdebug,
84 MYCONFIG => \%::myconfig,
89 my $parser = $self->get_template;
90 $parser->process($source, \%params, \$output) || croak $parser->error;
92 return SL::Presenter::EscapedText->new(text => $output, is_escaped => 1);
99 Template->new({ INTERPOLATE => 0,
103 PLUGIN_BASE => 'SL::Template::Plugin',
104 INCLUDE_PATH => '.:templates/webpages',
105 COMPILE_EXT => '.tcc',
106 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
107 ERROR => 'templates/webpages/generic/exception.html',
110 return $self->{template};
114 my ($self, $text) = @_;
116 return SL::Presenter::EscapedText->new(text => $text);
120 my ($self, $text) = @_;
122 return SL::Presenter::EscapedText->new(text => $text, is_escaped => 1);
131 SL::Presenter - presentation layer class
136 my $presenter = SL::Presenter->get;
138 # Lower-level template parsing:
139 my $html = $presenter->render(
140 'presenter/dir/template.html',
144 # Higher-level rendering of certain objects:
145 use SL::DB::Customer;
147 my $linked_customer_name = $presenter->customer($customer, display => 'table-cell');
149 # Render a list of links to sales/purchase records:
152 my $quotation = SL::DB::Manager::Order->get_first(where => { quotation => 1 });
153 my $records = $quotation->linked_records(direction => 'to');
154 my $html = $presenter->grouped_record_list($records);
156 =head1 CLASS FUNCTIONS
162 Returns the global presenter object and creates it if it doesn't exist
167 =head1 INSTANCE FUNCTIONS
171 =item C<render $template, [ $options, ] %locals>
173 Renders the template C<$template>. Provides other variables than
174 C<Form::parse_html_template> does.
176 C<$options>, if present, must be a hash reference. All remaining
177 parameters are slurped into C<%locals>.
179 This is the backend function that L<SL::Controller::Base/render>
180 calls. The big difference is that the presenter's L<render> function
181 always returns the input and never sends anything to the browser while
182 the controller's function usually sends the result to the
183 controller. Therefore the presenter's L<render> function does not use
184 all of the parameters for controlling the output that the controller's
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.
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.
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.
198 Other reference types, unknown options and unknown arguments to the
199 C<type> option cause the function to L<croak>.
201 The following options are available:
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.
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.
219 If template processing is requested then the template has access to
220 the following variables:
224 =item * C<AUTH> -- C<$::auth>
226 =item * C<FORM> -- C<$::form>
228 =item * C<LOCALE> -- C<$::locale>
230 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
231 with the same name they appear in the file (first level is the
232 section, second the actual variable, e.g. C<system.dbcharset>,
233 C<features.webdav> etc)
235 =item * C<LXDEBUG> -- C<$::lxdebug>
237 =item * C<MYCONFIG> -- C<%::myconfig>
239 =item * C<SELF> -- the controller instance
241 =item * All items from C<%locals>
245 The function will always return the output and never send anything to
248 Example: Render a HTML template with a certain title and a few locals
250 $presenter->render('todo/list',
251 title => 'List TODO items',
252 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
254 Example: Render a string and return its content for further processing
255 by the calling function.
257 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
259 Example: Return the content of a JSON template file without processing
262 my $template_content = $presenter->render(
264 { type => 'json', process => 0 }
267 =item C<escape $text>
269 Returns an HTML-escaped version of C<$text>. Instead of a string an
270 instance of the thin proxy-object L<SL::Presenter::EscapedText> is
273 It is safe to call C<escape> on an instance of
274 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
277 =item C<escaped_text $text>
279 Returns an instance of L<SL::Presenter::EscapedText>. C<$text> is
280 assumed to be a string that has already been HTML-escaped.
282 It is safe to call C<escaped_text> on an instance of
283 L<SL::Presenter::EscapedText>. This is a no-op (the same instance will
286 =item C<get_template>
288 Returns the global instance of L<Template> and creates it if it
289 doesn't exist already.
295 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>