1 package SL::Template::Plugin::L;
 
   3 use base qw( Template::Plugin );
 
   8 { # This will give you an id for identifying html tags and such.
 
   9   # It's guaranteed to be unique unless you exceed 10 mio calls per request.
 
  10   # Do not use these id's to store information across requests.
 
  11 my $_id_sequence = int rand 1e7;
 
  13   return $_id_sequence = ($_id_sequence + 1) % 1e7;
 
  19   return $::locale->quote_special_chars('HTML', $string);
 
  23   return (@_ && (ref($_[0]) eq 'HASH')) ? %{ $_[0] } : @_;
 
  30   return bless { }, $class;
 
  37   $name    =~ s/[^\w_]/_/g;
 
  45   my %options = _hashify(@_);
 
  48   while (my ($name, $value) = each %options) {
 
  51     push @result, _H($name) . '="' . _H($value) . '"';
 
  54   return @result ? ' ' . join(' ', @result) : '';
 
  61   my $attributes = $self->attributes(@_);
 
  63   return "<${tag}${attributes}/>" unless defined($content);
 
  64   return "<${tag}${attributes}>${content}</${tag}>";
 
  70   my $options_str     = shift;
 
  71   my %attributes      = _hashify(@_);
 
  73   $attributes{id}   ||= $self->name_to_id($name);
 
  75   return $self->html_tag('select', $options_str, %attributes, name => $name);
 
  82   my %attributes      = _hashify(@_);
 
  84   $attributes{id}   ||= $self->name_to_id($name);
 
  85   $content            = $content ? '' : _H($content);
 
  87   return $self->html_tag('textarea', $content, %attributes, name => $name);
 
  93   my %attributes       = _hashify(@_);
 
  95   $attributes{id}    ||= $self->name_to_id($name);
 
  96   $attributes{value}   = 1 unless defined $attributes{value};
 
  97   my $label            = delete $attributes{label};
 
  99   if ($attributes{checked}) {
 
 100     $attributes{checked} = 'checked';
 
 102     delete $attributes{checked};
 
 105   my $code  = $self->html_tag('input', undef,  %attributes, name => $name, type => 'checkbox');
 
 106   $code    .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
 
 111 sub radio_button_tag {
 
 114   my %attributes       = _hashify(@_);
 
 116   $attributes{value}   = 1 unless defined $attributes{value};
 
 117   $attributes{id}    ||= $self->name_to_id($name . "_" . $attributes{value});
 
 118   my $label            = delete $attributes{label};
 
 120   if ($attributes{checked}) {
 
 121     $attributes{checked} = 'checked';
 
 123     delete $attributes{checked};
 
 126   my $code  = $self->html_tag('input', undef,  %attributes, name => $name, type => 'radio');
 
 127   $code    .= $self->html_tag('label', $label, for => $attributes{id}) if $label;
 
 136   my %attributes      = _hashify(@_);
 
 138   $attributes{id}   ||= $self->name_to_id($name);
 
 139   $attributes{type} ||= 'text';
 
 141   return $self->html_tag('input', undef, %attributes, name => $name, value => $value);
 
 144 sub options_for_select {
 
 146   my $collection    = shift;
 
 147   my %options       = _hashify(@_);
 
 149   my $value_key     = $options{value} || 'id';
 
 150   my $title_key     = $options{title} || $value_key;
 
 153   push @elements, [ undef, $options{empty_title} || '' ] if $options{with_empty};
 
 155   if ($collection && (ref $collection eq 'ARRAY')) {
 
 156     foreach my $element (@{ $collection }) {
 
 157       my @result = !ref $element            ? ( $element,               $element               )
 
 158                  :  ref $element eq 'ARRAY' ? ( $element->[0],          $element->[1]          )
 
 159                  :  ref $element eq 'HASH'  ? ( $element->{$value_key}, $element->{$title_key} )
 
 160                  :                            ( $element->$value_key,   $element->$title_key   );
 
 162       push @elements, \@result;
 
 167   foreach my $result (@elements) {
 
 168     my %attributes = ( value => $result->[0] );
 
 169     $attributes{selected} = 'selected' if $options{default} && ($options{default} eq ($result->[0] || ''));
 
 171     $code .= $self->html_tag('option', _H($result->[1]), %attributes);
 
 178   my ($self, $data) = @_;
 
 179   return $self->html_tag('script', $data, type => 'text/javascript');
 
 183   my ($self, $name, $value, @slurp) = @_;
 
 184   my %params   = _hashify(@slurp);
 
 185   my $name_e   = _H($name);
 
 188   $params{cal_align} ||= 'BR';
 
 190   $self->input_tag($name, $value,
 
 193     title  => _H($::myconfig{dateformat}),
 
 194     onBlur => 'check_right_date_format(this)',
 
 196   ) . ((!$params{no_cal}) ?
 
 197   $self->html_tag('img', undef,
 
 198     src    => 'image/calendar.png',
 
 200     title  => _H($::myconfig{dateformat}),
 
 204     "Calendar.setup({ inputField: '$name_e', ifFormat: '$::myconfig{jsc_dateformat}', align: '$params{cal_align}', button: 'trigger$seq'  });"
 
 214 SL::Templates::Plugin::L -- Layouting / tag generation
 
 218 Usage from a template:
 
 222   [% L.select_tag('direction', [ [ 'left', 'To the left' ], [ 'right', 'To the right' ] ]) %]
 
 224   [% L.select_tag('direction', L.options_for_select([ { direction => 'left',  display => 'To the left'  },
 
 225                                                       { direction => 'right', display => 'To the right' } ],
 
 226                                                     value => 'direction', title => 'display', default => 'right')) %]
 
 230 A module modeled a bit after Rails' ActionView helpers. Several small
 
 231 functions that create HTML tags from various kinds of data sources.
 
 235 =head2 LOW-LEVEL FUNCTIONS
 
 239 =item C<name_to_id $name>
 
 241 Converts a name to a HTML id by replacing various characters.
 
 243 =item C<attributes %items>
 
 245 Creates a string from all elements in C<%items> suitable for usage as
 
 246 HTML tag attributes. Keys and values are HTML escaped even though keys
 
 247 must not contain non-ASCII characters for browsers to accept them.
 
 249 =item C<html_tag $tag_name, $content_string, %attributes>
 
 251 Creates an opening and closing HTML tag for C<$tag_name> and puts
 
 252 C<$content_string> between the two. If C<$content_string> is undefined
 
 253 or empty then only a E<lt>tag/E<gt> tag will be created. Attributes
 
 254 are key/value pairs added to the opening tag.
 
 256 C<$content_string> is not HTML escaped.
 
 260 =head2 HIGH-LEVEL FUNCTIONS
 
 264 =item C<select_tag $name, $options_string, %attributes>
 
 266 Creates a HTML 'select' tag named C<$name> with the contents
 
 267 C<$options_string> and with arbitrary HTML attributes from
 
 268 C<%attributes>. The tag's C<id> defaults to C<name_to_id($name)>.
 
 270 The $options_string is usually created by the C<options_for_select>
 
 273 =item C<input_tag $name, $value, %attributes>
 
 275 Creates a HTML 'input type=text' tag named C<$name> with the value
 
 276 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
 
 277 tag's C<id> defaults to C<name_to_id($name)>.
 
 279 =item C<textarea_tag $name, $value, %attributes>
 
 281 Creates a HTML 'textarea' tag named C<$name> with the content
 
 282 C<$value> and with arbitrary HTML attributes from C<%attributes>. The
 
 283 tag's C<id> defaults to C<name_to_id($name)>.
 
 285 =item C<checkbox_tag $name, %attributes>
 
 287 Creates a HTML 'input type=checkbox' tag named C<$name> with arbitrary
 
 288 HTML attributes from C<%attributes>. The tag's C<id> defaults to
 
 289 C<name_to_id($name)>. The tag's C<value> defaults to C<1>.
 
 291 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
 
 292 created with said C<label>. No attribute named C<label> is created in
 
 295 =item C<date_tag $name, $value, cal_align =E<gt> $align_code, %attributes>
 
 297 Creates a date input field, with an attached javascript that will open a
 
 298 calendar on click. The javascript ist by default anchoered at the bottom right
 
 299 sight. This can be overridden with C<cal_align>, see Calendar documentation for
 
 300 the details, usually you'll want a two letter abbreviation of the alignment.
 
 301 Right + Bottom becomes C<BL>.
 
 303 =item C<radio_button_tag $name, %attributes>
 
 305 Creates a HTML 'input type=radio' tag named C<$name> with arbitrary
 
 306 HTML attributes from C<%attributes>. The tag's C<value> defaults to
 
 307 C<1>. The tag's C<id> defaults to C<name_to_id($name . "_" . $value)>.
 
 309 If C<%attributes> contains a key C<label> then a HTML 'label' tag is
 
 310 created with said C<label>. No attribute named C<label> is created in
 
 315 =head2 CONVERSION FUNCTIONS
 
 319 =item C<options_for_select \@collection, %options>
 
 321 Creates a string suitable for a HTML 'select' tag consisting of one
 
 322 'E<lt>optionE<gt>' tag for each element in C<\@collection>. The value
 
 323 to use and the title to display are extracted from the elements in
 
 324 C<\@collection>. Each element can be one of four things:
 
 328 =item 1. An array reference with at least two elements. The first element is
 
 329 the value, the second element is its title.
 
 331 =item 2. A scalar. The scalar is both the value and the title.
 
 333 =item 3. A hash reference. In this case C<%options> must contain
 
 334 I<value> and I<title> keys that name the keys in the element to use
 
 335 for the value and title respectively.
 
 337 =item 4. A blessed reference. In this case C<%options> must contain
 
 338 I<value> and I<title> keys that name functions called on the blessed
 
 339 reference whose return values are used as the value and title
 
 344 For cases 3 and 4 C<$options{value}> defaults to C<id> and
 
 345 C<$options{title}> defaults to C<$options{value}>.
 
 347 If the option C<with_empty> is set then an empty element (value
 
 348 C<undef>) will be used as the first element. The title to display for
 
 349 this element can be set with the option C<empty_title> and defaults to
 
 354 =head1 MODULE AUTHORS
 
 356 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
 
 358 L<http://linet-services.de>