5 use parent qw(Rose::Object);
9 use List::Util qw(first);
11 use SL::Presenter::EscapedText qw(is_escaped);
13 use Rose::Object::MakeMethods::Generic (
14 scalar => [ qw(need_reinit_widgets) ],
18 return $::request->presenter;
24 my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_);
26 # Set defaults for all available options.
31 $options->{$_} //= $defaults{$_} for keys %defaults;
32 $options->{type} = lc $options->{type};
34 # Check supplied options for validity.
35 foreach (keys %{ $options }) {
36 croak "Unsupported option: $_" unless $defaults{$_};
39 # Only certain types are supported.
40 croak "Unsupported type: " . $options->{type} unless $options->{type} =~ m/^(?:html|js|json|text)$/;
42 # The "template" argument must be a string or a reference to one.
43 $template = ${ $template } if ((ref($template) || '') eq 'REF') && (ref(${ $template }) eq 'SL::Presenter::EscapedText');
44 croak "Unsupported 'template' reference type: " . ref($template) if ref($template) && (ref($template) !~ m/^(?:SCALAR|SL::Presenter::EscapedText)$/);
46 # Look for the file given by $template if $template is not a reference.
49 my $webpages_path = $::request->layout->webpages_path;
50 my $webpages_fallback = $::request->layout->webpages_fallback_path;
52 my $ext = $options->{type} eq 'text' ? 'txt' : $options->{type};
54 $source = first { -f } map { "${_}/${template}.${ext}" } grep { defined } $webpages_path, $webpages_fallback;
56 croak "Template file ${template} not found" unless $source;
58 } elsif (ref($template) eq 'SCALAR') {
59 # Normal scalar reference: hand over to Template
63 # Instance of SL::Presenter::EscapedText. Get reference to its content.
64 $source = \$template->{text};
67 # If no processing is requested then return the content.
68 if (!$options->{process}) {
69 # If $template is a reference then don't try to read a file.
70 my $ref = ref $template;
71 return $template if $ref eq 'SL::Presenter::EscapedText';
72 return is_escaped(${ $template }) if $ref eq 'SCALAR';
74 # Otherwise return the file's content.
75 my $file = IO::File->new($source, "r") || croak("Template file ${source} could not be read");
76 my $content = do { local $/ = ''; <$file> };
79 return is_escaped($content);
82 # Processing was requested. Set up all variables.
83 my %params = ( %locals,
85 FLASH => $::form->{FLASH},
87 INSTANCE_CONF => $::instance_conf,
89 LXCONFIG => \%::lx_office_conf,
90 LXDEBUG => $::lxdebug,
91 MYCONFIG => \%::myconfig,
96 my $parser = $self->get_template;
97 $parser->process($source, \%params, \$output) || croak $parser->error;
99 return is_escaped($output);
105 my $webpages_path = $::request->layout->webpages_path;
106 my $webpages_fallback = $::request->layout->webpages_fallback_path;
108 my $include_path = join ':', grep defined, $webpages_path, $webpages_fallback;
110 # Make locales.pl parse generic/exception.html, too:
111 # $::form->parse_html_template("generic/exception")
112 $self->{template} ||=
113 Template->new({ INTERPOLATE => 0,
117 PLUGIN_BASE => 'SL::Template::Plugin',
118 INCLUDE_PATH => ".:$include_path",
119 COMPILE_EXT => '.tcc',
120 COMPILE_DIR => $::lx_office_conf{paths}->{userspath} . '/templates-cache',
121 ERROR => "${webpages_path}/generic/exception.html",
125 return $self->{template};
134 SL::Presenter - presentation layer class
139 my $presenter = SL::Presenter->get;
141 # Lower-level template parsing:
142 my $html = $presenter->render(
143 'presenter/dir/template.html',
147 # Higher-level rendering of certain objects:
148 use SL::DB::Customer;
150 my $linked_customer_name = $customer->presenter->customer(display => 'table-cell');
152 # Render a list of links to sales/purchase records:
154 use SL::Presenter::Record qw(grouped_record_list);
156 my $quotation = SL::DB::Manager::Order->get_first(
157 where => [ or => ['record_type' => 'sales_quotation',
158 'record_type' => 'request_quotation' ]]);
159 my $records = $quotation->linked_records(direction => 'to');
160 my $html = grouped_record_list($records);
162 =head1 CLASS FUNCTIONS
168 Returns the global presenter object and creates it if it doesn't exist
173 =head1 INSTANCE FUNCTIONS
177 =item C<render $template, [ $options, ] %locals>
179 Renders the template C<$template>. Provides other variables than
180 C<Form::parse_html_template> does.
182 C<$options>, if present, must be a hash reference. All remaining
183 parameters are slurped into C<%locals>.
185 This is the backend function that L<SL::Controller::Base/render>
186 calls. The big difference is that the presenter's L<render> function
187 always returns the input and never sends anything to the browser while
188 the controller's function usually sends the result to the
189 controller. Therefore the presenter's L<render> function does not use
190 all of the parameters for controlling the output that the controller's
193 What is rendered and how C<$template> is interpreted is determined
194 both by C<$template>'s reference type and by the supplied options.
196 If C<$template> is a normal scalar (not a reference) then it is meant
197 to be a template file name relative to the C<templates/webpages>
198 directory. The file name to use is determined by the C<type> option.
200 If C<$template> is a reference to a scalar then the referenced
201 scalar's content is used as the content to process. The C<type> option
202 is not considered in this case.
204 C<$template> can also be an instance of L<SL::Presenter::EscapedText>
205 or a reference to such an instance. Both of these cases are handled
206 the same way as if C<$template> were a reference to a scalar: its
207 content is processed, and C<type> is not considered.
209 Other reference types, unknown options and unknown arguments to the
210 C<type> option cause the function to L<croak>.
212 The following options are available:
218 The template type. Can be C<html> (the default), C<js> for JavaScript,
219 C<json> for JSON and C<text> for plain text content. Affects only the
220 extension that's added to the file name given with a non-reference
221 C<$template> argument.
225 If trueish (which is also the default) it causes the template/content
226 to be processed by the Template toolkit. Otherwise the
227 template/content is returned as-is.
231 If template processing is requested then the template has access to
232 the following variables:
236 =item * C<AUTH> -- C<$::auth>
238 =item * C<FLASH> -- the flash instance (C<$::form-E<gt>{FLASH}>)
240 =item * C<FORM> -- C<$::form>
242 =item * C<INSTANCE_CONF> -- C<$::instance_conf>
244 =item * C<LOCALE> -- C<$::locale>
246 =item * C<LXCONFIG> -- all parameters from C<config/kivitendo.conf>
247 with the same name they appear in the file (first level is the
248 section, second the actual variable, e.g. C<system.language>,
249 C<features.webdav> etc)
251 =item * C<LXDEBUG> -- C<$::lxdebug>
253 =item * C<MYCONFIG> -- C<%::myconfig>
255 =item * C<SELF> -- the controller instance
257 =item * C<PRESENTER> -- the presenter instance the template is
260 =item * All items from C<%locals>
264 The function will always return the output and never send anything to
267 Example: Render a HTML template with a certain title and a few locals
269 $presenter->render('todo/list',
270 title => 'List TODO items',
271 TODO_ITEMS => SL::DB::Manager::Todo->get_all_sorted);
273 Example: Render a string and return its content for further processing
274 by the calling function.
276 my $content = $presenter->render(\'[% USE JavaScript %][% JavaScript.replace_with("#someid", "js/something") %]');
278 Example: Return the content of a JSON template file without processing
281 my $template_content = $presenter->render(
283 { type => 'json', process => 0 }
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>