Revision bdeaaa35
Von Moritz Bunkus vor fast 14 Jahren hinzugefügt
SL/Controller/Base.pm | ||
---|---|---|
2 | 2 |
|
3 | 3 |
use parent qw(Rose::Object); |
4 | 4 |
|
5 |
use Carp; |
|
5 | 6 |
use List::Util qw(first); |
6 | 7 |
|
7 | 8 |
# |
8 | 9 |
# public/helper functions |
9 | 10 |
# |
10 | 11 |
|
11 |
sub parse_html_template { |
|
12 |
my $self = shift; |
|
13 |
my $name = shift; |
|
14 |
my $locals = shift || {}; |
|
15 |
|
|
16 |
return $::form->parse_html_template($name, { %{ $locals }, SELF => $self }); |
|
17 |
} |
|
18 |
|
|
19 | 12 |
sub url_for { |
20 | 13 |
my $self = shift; |
21 | 14 |
|
... | ... | |
38 | 31 |
} |
39 | 32 |
|
40 | 33 |
sub render { |
41 |
my ($self, $template, %params) = @_; |
|
34 |
my $self = shift; |
|
35 |
my $template = shift; |
|
36 |
my ($options, %locals) = (@_ && ref($_[0])) ? @_ : ({ }, @_); |
|
37 |
|
|
38 |
my $source; |
|
39 |
if ($options->{inline}) { |
|
40 |
$source = \$template; |
|
41 |
|
|
42 |
} else { |
|
43 |
$source = "templates/webpages/${template}.html"; |
|
44 |
croak "Template file ${source} not found" unless -f $source; |
|
45 |
} |
|
42 | 46 |
|
43 |
if ($params{title}) {
|
|
44 |
$::form->{title} = delete $params{title};
|
|
47 |
if (!$options->{partial} && !$options->{inline}) {
|
|
48 |
$::form->{title} = $locals{title} if $locals{title};
|
|
45 | 49 |
$::form->header; |
46 | 50 |
} |
47 | 51 |
|
48 |
print $self->parse_html_template($template, $params{locals}); |
|
52 |
my %params = ( %locals, |
|
53 |
AUTH => $::auth, |
|
54 |
FORM => $::form, |
|
55 |
LOCALE => $::locale, |
|
56 |
LXCONFIG => { dbcharset => $::dbcharset, |
|
57 |
webdav => $::webdav, |
|
58 |
lizenzen => $::lizenzen, |
|
59 |
latex_templates => $::latex, |
|
60 |
opendocument_templates => $::opendocument_templates, |
|
61 |
vertreter => $::vertreter, |
|
62 |
show_best_before => $::show_best_before, |
|
63 |
}, |
|
64 |
LXDEBUG => $::lxdebug, |
|
65 |
MYCONFIG => \%::myconfig, |
|
66 |
SELF => $self, |
|
67 |
); |
|
68 |
|
|
69 |
my $output; |
|
70 |
my $parser = $self->_template_obj; |
|
71 |
$parser->process($source, \%params, \$output) || croak $parser->error; |
|
72 |
|
|
73 |
print $output unless $options->{inline}; |
|
74 |
|
|
75 |
return $output; |
|
49 | 76 |
} |
50 | 77 |
|
51 | 78 |
# |
... | ... | |
75 | 102 |
$self->$action(@_); |
76 | 103 |
} |
77 | 104 |
|
105 |
sub _template_obj { |
|
106 |
my ($self) = @_; |
|
107 |
|
|
108 |
$self->{__basepriv_template_obj} ||= |
|
109 |
Template->new({ INTERPOLATE => 0, |
|
110 |
EVAL_PERL => 0, |
|
111 |
ABSOLUTE => 1, |
|
112 |
CACHE_SIZE => 0, |
|
113 |
PLUGIN_BASE => 'SL::Template::Plugin', |
|
114 |
INCLUDE_PATH => '.:templates/webpages', |
|
115 |
COMPILE_EXT => '.tcc', |
|
116 |
COMPILE_DIR => $::userspath . '/templates-cache', |
|
117 |
}) || croak; |
|
118 |
|
|
119 |
return $self->{__basepriv_template_obj}; |
|
120 |
} |
|
121 |
|
|
78 | 122 |
1; |
79 | 123 |
|
80 | 124 |
__END__ |
... | ... | |
162 | 206 |
|
163 | 207 |
=over 4 |
164 | 208 |
|
165 |
=item C<parse_html_template $file_name, $local_variables> |
|
209 |
=item C<render $template, [ $options, ] %locals> |
|
210 |
|
|
211 |
Renders the template C<$template>. Provides other variables than |
|
212 |
C<Form::parse_html_template> does. |
|
213 |
|
|
214 |
C<$options>, if present, must be a hash reference. All remaining |
|
215 |
parameters are slurped into C<%locals>. |
|
216 |
|
|
217 |
What is rendered and how C<$template> is interpreted is determined by |
|
218 |
C<< $options->{inline} >> and C<< $options->{partial} >>. |
|
219 |
|
|
220 |
If C<< $options->{inline} >> is trueish then C<$template> is a string |
|
221 |
containing the template code to interprete. Additionally the output |
|
222 |
will not be sent to the browser. Instead it is only returned to the |
|
223 |
caller. |
|
166 | 224 |
|
167 |
Outputs an HTML template. It is a thin wrapper around |
|
168 |
C<Form::parse_html_template> which also adds the current object as the |
|
169 |
template variable C<SELF>. |
|
225 |
If C<< $options->{inline} >> is falsish then C<$template> is |
|
226 |
interpreted as the name of a template file. It is prefixed with |
|
227 |
"templates/webpages/" and postfixed with ".html". An exception will be |
|
228 |
thrown if that file does not exist. |
|
170 | 229 |
|
171 |
=item C<render $template, %params> |
|
230 |
If C<< $options->{partial} >> is trueish then C<< $::form->header >> |
|
231 |
will not be called. Otherwise C<< $::form->{header} >> will be set to |
|
232 |
C<$locals{header}> (only if $locals{header} is trueish) and |
|
233 |
C<< $::form->header >> will be called before the template itself is |
|
234 |
processed. |
|
235 |
|
|
236 |
The template itself has access to the following variables: |
|
237 |
|
|
238 |
=over 2 |
|
239 |
|
|
240 |
=item * C<AUTH> -- C<$::auth> |
|
241 |
|
|
242 |
=item * C<FORM> -- C<$::form> |
|
243 |
|
|
244 |
=item * C<LOCALE> -- C<$::locale> |
|
245 |
|
|
246 |
=item * C<LXCONFIG> -- all parameters from C<config/lx-erp.conf> with |
|
247 |
the same name they appear in the file (e.g. C<dbcharset>, C<webdav> |
|
248 |
etc) |
|
249 |
|
|
250 |
=item * C<LXDEBUG> -- C<$::lxdebug> |
|
251 |
|
|
252 |
=item * C<MYCONFIG> -- C<%::myconfig> |
|
253 |
|
|
254 |
=item * C<SELF> -- the controller instance |
|
255 |
|
|
256 |
=item * All items from C<%locals> |
|
257 |
|
|
258 |
=back |
|
172 | 259 |
|
173 |
Renders the template C<$template> by calling |
|
174 |
L</parse_html_template>. C<$params{locals}> will be used as the second |
|
175 |
parameter to L</parse_html_template>. |
|
260 |
Unless C<< $options->{inline} >> is trueish the function will send the |
|
261 |
output to the browser. |
|
176 | 262 |
|
177 |
If C<$params{title}> is trueish then the function also sets |
|
178 |
C<< $::form->{header} >> to that value and calls C<< $::form->header >>. |
|
263 |
The function will always return the output. |
|
179 | 264 |
|
180 | 265 |
=item C<url_for $url> |
181 | 266 |
|
Auch abrufbar als: Unified diff
API-Umstellung von SL::Controller::Base::render
Siehe Dokumentation im File, wie das API nun aussieht.