1 package SL::Template::Plugin::L;
3 use base qw( Template::Plugin );
10 return $::locale->quote_special_chars('HTML', $string);
17 return bless { }, $class;
22 my $options = shift || {};
25 while (my ($name, $value) = each %{ $options }) {
28 push @result, _H($name) . '="' . _H($value) . '"';
31 return @result ? ' ' . join(' ', @result) : '';
38 my $attributes = $self->attributes(shift || {});
40 return "<${tag}${attributes}/>" unless $content;
41 return "<${tag}${attributes}>${content}</${tag}>";
47 my $options_str = shift;
48 my $attributes = shift || {};
50 $attributes->{name} = $name;
51 $attributes->{id} ||= $name;
53 return $self->html_tag('select', $options_str, $attributes);
56 sub options_for_select {
58 my $collection = shift;
59 my $options = shift || {};
61 my $value_key = $options->{value} || 'id';
62 my $title_key = $options->{title} || $value_key;
65 if ($collection && (ref $collection eq 'ARRAY')) {
66 foreach my $element (@{ $collection }) {
67 my @result = !ref $element ? ( $element, $element )
68 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
69 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
70 : ( $element->$value_key, $element->$title_key );
72 my %attributes = ( value => $result[0] );
73 $attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || ''));
75 push @tags, $self->html_tag('option', _H($result[1]), \%attributes);
79 return join('', @tags);
88 SL::Templates::Plugin::L -- Layouting / tag generation
92 Usage from a template:
96 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
98 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
99 { direction => 'right', display => 'To the right' } ],
100 value => 'direction', title => 'display', default => 'right')) %]
104 A module modeled a bit after Rails' ActionView helpers. Several small
105 functions that create HTML tags from various kinds of data sources.
111 =item C<attributes \%items>
113 Creates a string from all elements in C<\%items> suitable for usage as
114 HTML tag attributes. Keys and values are HTML escaped even though keys
115 must not contain non-ASCII characters for browsers to accept them.
117 =item C<html_tag $tag_name, $content_string, \%attributes>
119 Creates an opening and closing HTML tag for C<$tag_name> and puts
120 C<$content_string> between the two. If C<$content_string> is undefined
121 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
122 are key/value pairs added to the opening tag.
124 =item C<options_for_select \@collection, \%options>
126 Creates a string suitable for a HTML 'select' tag consisting of one
127 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
128 to use and the title to display are extracted from the elements in
129 C<\@collection>. Each element can be one of four things:
133 =item 1. An array reference with at least two elements. The first element is
134 the value, the second element is its title.
136 =item 2. A scalar. The scalar is both the value and the title.
138 =item 3. A hash reference. In this case C<\%options> must contain
139 I<value> and I<title> keys that name the keys in the element to use
140 for the value and title respectively.
142 =item 4. A blessed reference. In this case C<\%options> must contain
143 I<value> and I<title> keys that name functions called on the blessed
144 reference whose return values are used as the value and title
149 For cases 3 and 4 C<$options{value}> defaults to C<id> and
150 C<$options{title}> defaults to C<$options{value}>.
152 =item C<select_tag $name, $options_string, \%attributes>
154 Creates a HTML 'select' tag named $name with the contents
155 $options_string and with arbitrary HTML attributes from
156 C<\%attributes>. The tag's C<id> defaults to C<$name>.
158 The $options_string is usually created by the C<options_for_select>
163 =head1 MODULE AUTHORS
165 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
167 L<http://linet-services.de>