1 package SL::Template::Plugin::L;
3 use base qw( Template::Plugin );
12 return bless { }, $class;
17 my $options = shift || {};
20 while (my ($name, $value) = each %{ $options }) {
23 push @result, "${name}=\"" . $::locale->quote_special_chars('HTML', $value) . '"';
26 return @result ? ' ' . join(' ', @result) : '';
33 my $attributes = $self->attributes(shift || {});
35 return "<${tag}${attributes}/>" unless $content;
36 return "<${tag}${attributes}>${content}</${tag}>";
42 my $options_str = shift;
43 my $attributes = shift || {};
45 $attributes->{name} = $name;
46 $attributes->{id} ||= $name;
48 return $self->html_tag('select', $options_str, $attributes);
51 sub options_for_select {
53 my $collection = shift;
54 my $options = shift || {};
56 my $value_key = $options->{value} || 'id';
57 my $title_key = $options->{title} || $value_key;
60 if ($collection && (ref $collection eq 'ARRAY')) {
61 foreach my $element (@{ $collection }) {
62 my @result = !ref $element ? ( $element, $element )
63 : ref $element eq 'ARRAY' ? ( $element->[0], $element->[1] )
64 : ref $element eq 'HASH' ? ( $element->{$value_key}, $element->{$title_key} )
65 : ( $element->$value_key, $element->$title_key );
67 my %attributes = ( value => $result[0] );
68 $attributes{selected} = 'selected' if $options->{default} && ($options->{default} eq ($result[0] || ''));
70 push @tags, $self->html_tag('option', $result[1], \%attributes);
74 return join('', @tags);
83 SL::Templates::Plugin::L -- Layouting / tag generation
87 Usage from a template:
91 [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
93 [% L.select_tag('direction', L.options_for_select([ { direction => 'left', display => 'To the left' },
94 { direction => 'right', display => 'To the right' } ],
95 value => 'direction', title => 'display', default => 'right')) %]
99 A module modeled a bit after Rails' ActionView helpers. Several small
100 functions that create HTML tags from various kinds of data sources.
106 =item C<attributes \%items>
108 Creates a string from all elements in C<\%items> suitable for usage as
109 HTML tag attributes. Keys and values are HTML escaped even though keys
110 must not contain non-ASCII characters for browsers to accept them.
112 =item C<html_tag $tag_name, $content_string, \%attributes>
114 Creates an opening and closing HTML tag for C<$tag_name> and puts
115 C<$content_string> between the two. If C<$content_string> is undefined
116 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
117 are key/value pairs added to the opening tag.
119 =item C<options_for_select \@collection, \%options>
121 Creates a string suitable for a HTML 'select' tag consisting of one
122 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
123 to use and the title to display are extracted from the elements in
124 C<\@collection>. Each element can be one of four things:
128 =item 1. An array reference with at least two elements. The first element is
129 the value, the second element is its title.
131 =item 2. A scalar. The scalar is both the value and the title.
133 =item 3. A hash reference. In this case C<\%options> must contain
134 I<value> and I<title> keys that name the keys in the element to use
135 for the value and title respectively.
137 =item 4. A blessed reference. In this case C<\%options> must contain
138 I<value> and I<title> keys that name functions called on the blessed
139 reference whose return values are used as the value and title
144 For cases 3 and 4 C<$options{value}> defaults to C<id> and
145 C<$options{title}> defaults to C<$options{value}>.
147 =item C<select_tag $name, $options_string, \%attributes>
149 Creates a HTML 'select' tag named $name with the contents
150 $options_string and with arbitrary HTML attributes from
151 C<\%attributes>. The tag's C<id> defaults to C<$name>.
153 The $options_string is usually created by the C<options_for_select>
158 =head1 MODULE AUTHORS
160 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
162 L<http://linet-services.de>