1 package SL::Template::Plugin::L;
3 use base qw( Template::Plugin );
5 use List::MoreUtils qw(apply);
9 { # This will give you an id for identifying html tags and such.
10 # It's guaranteed to be unique unless you exceed 10 mio calls per request.
11 # Do not use these id's to store information across requests.
12 my $_id_sequence = int rand 1e7;
14 return $_id_sequence = ($_id_sequence + 1) % 1e7;
20 return $::locale->quote_special_chars('HTML', $string);
24 return (@_ && (ref($_[0]) eq 'HASH')) ? %{ $_[0] } : @_;
31 return bless { }, $class;
38 $name =~ s/[^\w_]/_/g;
46 my %options = _hashify(@_);
49 while (my ($name, $value) = each %options) {
52 push @result, _H($name) . '="' . _H($value) . '"';
55 return @result ? ' ' . join(' ', @result) : '';
62 my $attributes = $self->attributes(@_);
64 return "<${tag}${attributes}/>" unless defined($content);
65 return "<${tag}${attributes}>${content}</${tag}>";
71 my $options_str = shift;
72 my %attributes = _hashify(@_);
74 $attributes{id} ||= $self->name_to_id($name);
75 $options_str = $self->options_for_select($options_str) if ref $options_str;
77 return $self->html_tag('select', $options_str, %attributes, name => $name);
84 my %attributes = _hashify(@_);
86 $attributes{id} ||= $self->name_to_id($name);
87 $content = $content ? '' : _H($content);
89 return $self->html_tag('textarea', $content, %attributes, name => $name);
95 my %attributes = _hashify(@_);
97 $attributes{id} ||= $self->name_to_id($name);
98 $attributes{value} = 1 unless defined $attributes{value};
99 my $label = delete $attributes{label};
101 if ($attributes{checked}) {
102 $attributes{checked} = 'checked';
104 delete $attributes{checked};
107 my $code = $self->html_tag('input', undef, %attributes, name => $name, type => 'checkbox');
108 $code .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
113 sub radio_button_tag {
116 my %attributes = _hashify(@_);
118 $attributes{value} = 1 unless defined $attributes{value};
119 $attributes{id} ||= $self->name_to_id($name . "_" . $attributes{value});
120 my $label = delete $attributes{label};
122 if ($attributes{checked}) {
123 $attributes{checked} = 'checked';
125 delete $attributes{checked};
128 my $code = $self->html_tag('input', undef, %attributes, name => $name, type => 'radio');
129 $code .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
138 my %attributes = _hashify(@_);
140 $attributes{id} ||= $self->name_to_id($name);
141 $attributes{type} ||= 'text';
143 return $self->html_tag('input', undef, %attributes, name => $name, value => $value);
147 return shift->input_tag(@_, type => 'hidden');
151 return shift->input_tag(@_, type => 'submit', class => 'submit');
154 sub options_for_select {
156 my $collection = shift;
157 my %options = _hashify(@_);
159 my $value_key = $options{value} || 'id';
160 my $title_key = $options{title} || $value_key;
163 push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
165 if ($collection && (ref $collection eq 'ARRAY')) {
166 foreach my $element (@{ $collection }) {
167 my @result = !ref $element ? ( $element, $element )
168 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
169 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
170 : ( $element->$value_key, $element->$title_key );
172 push @elements, \@result;
177 foreach my $result (@elements) {
178 my %attributes = ( value => $result->[0] );
179 $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || ''));
181 $code .= $self->html_tag('option', _H($result->[1]), %attributes);
188 my ($self, $data) = @_;
189 return $self->html_tag('script', $data, type => 'text/javascript');
193 my ($self, $name, $value, @slurp) = @_;
194 my %params = _hashify(@slurp);
195 my $name_e = _H($name);
197 my $datefmt = apply {
201 } $::myconfig{"dateformat"};
203 $params{cal_align} ||= 'BR';
205 $self->input_tag($name, $value,
208 title => _H($::myconfig{dateformat}),
209 onBlur => 'check_right_date_format(this)',
211 ) . ((!$params{no_cal}) ?
212 $self->html_tag('img', undef,
213 src => 'image/calendar.png',
215 title => _H($::myconfig{dateformat}),
219 "Calendar.setup({ inputField: '$name_e', ifFormat: '$datefmt', align: '$params{cal_align}', button: 'trigger$seq' });"
226 foreach my $file (@_) {
227 $file .= '.js' unless $file =~ m/\.js$/;
228 $file = "js/${file}" unless $file =~ m|/|;
230 $code .= qq|<script type="text/javascript" src="${file}"></script>|;
242 SL::Templates::Plugin::L -- Layouting / tag generation
246 Usage from a template:
250 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
252 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
253 { direction => 'right', display => 'To the right' } ],
254 value => 'direction', title => 'display', default => 'right')) %]
258 A module modeled a bit after Rails' ActionView helpers. Several small
259 functions that create HTML tags from various kinds of data sources.
263 =head2 LOW-LEVEL FUNCTIONS
267 =item C<name_to_id $name>
269 Converts a name to a HTML id by replacing various characters.
271 =item C<attributes %items>
273 Creates a string from all elements in C<%items> suitable for usage as
274 HTML tag attributes. Keys and values are HTML escaped even though keys
275 must not contain non-ASCII characters for browsers to accept them.
277 =item C<html_tag $tag_name, $content_string, %attributes>
279 Creates an opening and closing HTML tag for C<$tag_name> and puts
280 C<$content_string> between the two. If C<$content_string> is undefined
281 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
282 are key/value pairs added to the opening tag.
284 C<$content_string> is not HTML escaped.
288 =head2 HIGH-LEVEL FUNCTIONS
292 =item C<select_tag $name, $options_string, %attributes>
294 Creates a HTML 'select' tag named C<$name> with the contents
295 C<$options_string> and with arbitrary HTML attributes from
296 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
298 The C<$options_string> is usually created by the
299 L</options_for_select> function. If C<$options_string> is an array
300 reference then it will be passed to L</options_for_select>
303 =item C<input_tag $name, $value, %attributes>
305 Creates a HTML 'input type=text' tag named C<$name> with the value
306 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
307 tag's C<id> defaults to C<name_to_id($name)>.
309 =item C<textarea_tag $name, $value, %attributes>
311 Creates a HTML 'textarea' tag named C<$name> with the content
312 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
313 tag's C<id> defaults to C<name_to_id($name)>.
315 =item C<checkbox_tag $name, %attributes>
317 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
318 HTML attributes from C<%attributes>. The tag's C<id> defaults to
319 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
321 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
322 created with said C<label>. No attribute named C<label> is created in
325 =item C<date_tag $name, $value, cal_align =E<gt> $align_code, %attributes>
327 Creates a date input field, with an attached javascript that will open a
328 calendar on click. The javascript ist by default anchoered at the bottom right
329 sight. This can be overridden with C<cal_align>, see Calendar documentation for
330 the details, usually you'll want a two letter abbreviation of the alignment.
331 Right + Bottom becomes C<BL>.
333 =item C<radio_button_tag $name, %attributes>
335 Creates a HTML 'input type=radio' tag named C<$name> with arbitrary
336 HTML attributes from C<%attributes>. The tag's C<value> defaults to
337 C<1>. The tag's C<id> defaults to C<name_to_id($name . "_" . $value)>.
339 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
340 created with said C<label>. No attribute named C<label> is created in
343 =item C<javascript_tag $file1, $file2, $file3...>
345 Creates a HTML 'E<lt>script type="text/javascript" src="..."E<gt>'
346 tag for each file name parameter passed. Each file name will be
347 postfixed with '.js' if it isn't already and prefixed with 'js/' if it
348 doesn't contain a slash.
352 =head2 CONVERSION FUNCTIONS
356 =item C<options_for_select \@collection, %options>
358 Creates a string suitable for a HTML 'select' tag consisting of one
359 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
360 to use and the title to display are extracted from the elements in
361 C<\@collection>. Each element can be one of four things:
365 =item 1. An array reference with at least two elements. The first element is
366 the value, the second element is its title.
368 =item 2. A scalar. The scalar is both the value and the title.
370 =item 3. A hash reference. In this case C<%options> must contain
371 I<value> and I<title> keys that name the keys in the element to use
372 for the value and title respectively.
374 =item 4. A blessed reference. In this case C<%options> must contain
375 I<value> and I<title> keys that name functions called on the blessed
376 reference whose return values are used as the value and title
381 For cases 3 and 4 C<$options{value}> defaults to C<id> and
382 C<$options{title}> defaults to C<$options{value}>.
384 If the option C<with_empty> is set then an empty element (value
385 C<undef>) will be used as the first element. The title to display for
386 this element can be set with the option C<empty_title> and defaults to
391 =head1 MODULE AUTHORS
393 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
395 L<http://linet-services.de>