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 push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
113 if ($collection && (ref $collection eq 'ARRAY')) {
114 foreach my $element (@{ $collection }) {
115 my @result = !ref $element ? ( $element, $element )
116 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
117 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
118 : ( $element->$value_key, $element->$title_key );
120 push @elements, \@result;
125 foreach my $result (@elements) {
126 my %attributes = ( value => $result->[0] );
127 $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || ''));
129 $code .= $self->html_tag('option', _H($result->[1]), %attributes);
141 SL::Templates::Plugin::L -- Layouting / tag generation
145 Usage from a template:
149 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
151 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
152 { direction => 'right', display => 'To the right' } ],
153 value => 'direction', title => 'display', default => 'right')) %]
157 A module modeled a bit after Rails' ActionView helpers. Several small
158 functions that create HTML tags from various kinds of data sources.
162 =head2 LOW-LEVEL FUNCTIONS
166 =item C<name_to_id $name>
168 Converts a name to a HTML id by replacing various characters.
170 =item C<attributes %items>
172 Creates a string from all elements in C<%items> suitable for usage as
173 HTML tag attributes. Keys and values are HTML escaped even though keys
174 must not contain non-ASCII characters for browsers to accept them.
176 =item C<html_tag $tag_name, $content_string, %attributes>
178 Creates an opening and closing HTML tag for C<$tag_name> and puts
179 C<$content_string> between the two. If C<$content_string> is undefined
180 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
181 are key/value pairs added to the opening tag.
183 C<$content_string> is not HTML escaped.
187 =head2 HIGH-LEVEL FUNCTIONS
191 =item C<select_tag $name, $options_string, %attributes>
193 Creates a HTML 'select' tag named C<$name> with the contents
194 C<$options_string> and with arbitrary HTML attributes from
195 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
197 The $options_string is usually created by the C<options_for_select>
200 =item C<input_tag $name, $value, %attributes>
202 Creates a HTML 'input type=text' tag named C<$name> with the value
203 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
204 tag's C<id> defaults to C<name_to_id($name)>.
206 =item C<checkbox_tag $name, %attributes>
208 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
209 HTML attributes from C<%attributes>. The tag's C<id> defaults to
210 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
212 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
213 created with said C<label>. No attribute named C<label> is created in
218 =head2 CONVERSION FUNCTIONS
222 =item C<options_for_select \@collection, %options>
224 Creates a string suitable for a HTML 'select' tag consisting of one
225 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
226 to use and the title to display are extracted from the elements in
227 C<\@collection>. Each element can be one of four things:
231 =item 1. An array reference with at least two elements. The first element is
232 the value, the second element is its title.
234 =item 2. A scalar. The scalar is both the value and the title.
236 =item 3. A hash reference. In this case C<%options> must contain
237 I<value> and I<title> keys that name the keys in the element to use
238 for the value and title respectively.
240 =item 4. A blessed reference. In this case C<%options> must contain
241 I<value> and I<title> keys that name functions called on the blessed
242 reference whose return values are used as the value and title
247 For cases 3 and 4 C<$options{value}> defaults to C<id> and
248 C<$options{title}> defaults to C<$options{value}>.
250 If the option C<with_empty> is set then an empty element (value
251 C<undef>) will be used as the first element. The title to display for
252 this element can be set with the option C<empty_title> and defaults to
257 =head1 MODULE AUTHORS
259 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
261 L<http://linet-services.de>