1 package SL::Template::Plugin::L;
3 use base qw( Template::Plugin );
10 return $::locale->quote_special_chars('HTML', $string);
17 return bless { }, $class;
24 $name =~ s/[^\w_]/_/g;
32 my $options = shift || {};
35 while (my ($name, $value) = each %{ $options }) {
38 push @result, _H($name) . '="' . _H($value) . '"';
41 return @result ? ' ' . join(' ', @result) : '';
48 my $attributes = $self->attributes(shift || {});
50 return "<${tag}${attributes}/>" unless $content;
51 return "<${tag}${attributes}>${content}</${tag}>";
57 my $options_str = shift;
58 my $attributes = shift || {};
60 $attributes->{name} = $name;
61 $attributes->{id} ||= $self->name_to_id($name);
63 return $self->html_tag('select', $options_str, $attributes);
66 sub options_for_select {
68 my $collection = shift;
69 my $options = shift || {};
71 my $value_key = $options->{value} || 'id';
72 my $title_key = $options->{title} || $value_key;
75 if ($collection && (ref $collection eq 'ARRAY')) {
76 foreach my $element (@{ $collection }) {
77 my @result = !ref $element ? ( $element, $element )
78 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
79 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
80 : ( $element->$value_key, $element->$title_key );
82 my %attributes = ( value => $result[0] );
83 $attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || ''));
85 push @tags, $self->html_tag('option', _H($result[1]), \%attributes);
89 return join('', @tags);
98 SL::Templates::Plugin::L -- Layouting / tag generation
102 Usage from a template:
106 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
108 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
109 { direction => 'right', display => 'To the right' } ],
110 value => 'direction', title => 'display', default => 'right')) %]
114 A module modeled a bit after Rails' ActionView helpers. Several small
115 functions that create HTML tags from various kinds of data sources.
119 =head2 LOW-LEVEL FUNCTIONS
123 =item C<name_to_id $name>
125 Converts a name to a HTML id by replacing various characters.
127 =item C<attributes \%items>
129 Creates a string from all elements in C<\%items> suitable for usage as
130 HTML tag attributes. Keys and values are HTML escaped even though keys
131 must not contain non-ASCII characters for browsers to accept them.
133 =item C<html_tag $tag_name, $content_string, \%attributes>
135 Creates an opening and closing HTML tag for C<$tag_name> and puts
136 C<$content_string> between the two. If C<$content_string> is undefined
137 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
138 are key/value pairs added to the opening tag.
140 C<$content_string> is not HTML escaped.
144 =head2 HIGH-LEVEL FUNCTIONS
148 =item C<select_tag $name, $options_string, \%attributes>
150 Creates a HTML 'select' tag named $name with the contents
151 $options_string and with arbitrary HTML attributes from
152 C<\%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
154 The $options_string is usually created by the C<options_for_select>
159 =head2 CONVERSION FUNCTIONS
163 =item C<options_for_select \@collection, \%options>
165 Creates a string suitable for a HTML 'select' tag consisting of one
166 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
167 to use and the title to display are extracted from the elements in
168 C<\@collection>. Each element can be one of four things:
172 =item 1. An array reference with at least two elements. The first element is
173 the value, the second element is its title.
175 =item 2. A scalar. The scalar is both the value and the title.
177 =item 3. A hash reference. In this case C<\%options> must contain
178 I<value> and I<title> keys that name the keys in the element to use
179 for the value and title respectively.
181 =item 4. A blessed reference. In this case C<\%options> must contain
182 I<value> and I<title> keys that name functions called on the blessed
183 reference whose return values are used as the value and title
188 For cases 3 and 4 C<$options{value}> defaults to C<id> and
189 C<$options{title}> defaults to C<$options{value}>.
193 =head1 MODULE AUTHORS
195 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
197 L<http://linet-services.de>