1 package SL::Template::Plugin::L;
3 use base qw( Template::Plugin );
10 return $::locale->quote_special_chars('HTML', $string);
14 return (@_ && (ref($_[0]) eq 'HASH')) ? %{ $_[0] } : @_;
21 return bless { }, $class;
28 $name =~ s/[^\w_]/_/g;
36 my %options = _hashify(@_);
39 while (my ($name, $value) = each %options) {
42 push @result, _H($name) . '="' . _H($value) . '"';
45 return @result ? ' ' . join(' ', @result) : '';
52 my $attributes = $self->attributes(@_);
54 return "<${tag}${attributes}/>" unless $content;
55 return "<${tag}${attributes}>${content}</${tag}>";
61 my $options_str = shift;
62 my %attributes = _hashify(@_);
64 $attributes{id} ||= $self->name_to_id($name);
66 return $self->html_tag('select', $options_str, %attributes, name => $name);
72 my %attributes = _hashify(@_);
74 $attributes{id} ||= $self->name_to_id($name);
75 $attributes{value} = 1 unless defined $attributes{value};
76 my $label = delete $attributes{label};
78 if ($attributes{checked}) {
79 $attributes{checked} = 'checked';
81 delete $attributes{checked};
84 my $code = $self->html_tag('input', undef, %attributes, name => $name, type => 'checkbox');
85 $code .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
94 my %attributes = _hashify(@_);
96 $attributes{id} ||= $self->name_to_id($name);
97 $attributes{type} ||= 'text';
99 return $self->html_tag('input', undef, %attributes, name => $name, value => $value);
102 sub options_for_select {
104 my $collection = shift;
105 my %options = _hashify(@_);
107 my $value_key = $options{value} || 'id';
108 my $title_key = $options{title} || $value_key;
111 if ($collection && (ref $collection eq 'ARRAY')) {
112 foreach my $element (@{ $collection }) {
113 my @result = !ref $element ? ( $element, $element )
114 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
115 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
116 : ( $element->$value_key, $element->$title_key );
118 my %attributes = ( value => $result[0] );
119 $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result[0] || ''));
121 push @tags, $self->html_tag('option', _H($result[1]), %attributes);
125 return join('', @tags);
134 SL::Templates::Plugin::L -- Layouting / tag generation
138 Usage from a template:
142 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
144 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
145 { direction => 'right', display => 'To the right' } ],
146 value => 'direction', title => 'display', default => 'right')) %]
150 A module modeled a bit after Rails' ActionView helpers. Several small
151 functions that create HTML tags from various kinds of data sources.
155 =head2 LOW-LEVEL FUNCTIONS
159 =item C<name_to_id $name>
161 Converts a name to a HTML id by replacing various characters.
163 =item C<attributes %items>
165 Creates a string from all elements in C<%items> suitable for usage as
166 HTML tag attributes. Keys and values are HTML escaped even though keys
167 must not contain non-ASCII characters for browsers to accept them.
169 =item C<html_tag $tag_name, $content_string, %attributes>
171 Creates an opening and closing HTML tag for C<$tag_name> and puts
172 C<$content_string> between the two. If C<$content_string> is undefined
173 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
174 are key/value pairs added to the opening tag.
176 C<$content_string> is not HTML escaped.
180 =head2 HIGH-LEVEL FUNCTIONS
184 =item C<select_tag $name, $options_string, %attributes>
186 Creates a HTML 'select' tag named C<$name> with the contents
187 C<$options_string> and with arbitrary HTML attributes from
188 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
190 The $options_string is usually created by the C<options_for_select>
193 =item C<input_tag $name, $value, %attributes>
195 Creates a HTML 'input type=text' tag named C<$name> with the value
196 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
197 tag's C<id> defaults to C<name_to_id($name)>.
199 =item C<checkbox_tag $name, %attributes>
201 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
202 HTML attributes from C<%attributes>. The tag's C<id> defaults to
203 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
205 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
206 created with said C<label>. No attribute named C<label> is created in
211 =head2 CONVERSION FUNCTIONS
215 =item C<options_for_select \@collection, %options>
217 Creates a string suitable for a HTML 'select' tag consisting of one
218 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
219 to use and the title to display are extracted from the elements in
220 C<\@collection>. Each element can be one of four things:
224 =item 1. An array reference with at least two elements. The first element is
225 the value, the second element is its title.
227 =item 2. A scalar. The scalar is both the value and the title.
229 =item 3. A hash reference. In this case C<%options> must contain
230 I<value> and I<title> keys that name the keys in the element to use
231 for the value and title respectively.
233 =item 4. A blessed reference. In this case C<%options> must contain
234 I<value> and I<title> keys that name functions called on the blessed
235 reference whose return values are used as the value and title
240 For cases 3 and 4 C<$options{value}> defaults to C<id> and
241 C<$options{title}> defaults to C<$options{value}>.
245 =head1 MODULE AUTHORS
247 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
249 L<http://linet-services.de>