home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / HTML.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-30  |  5.0 KB  |  198 lines

  1. #============================================================= -*-Perl-*-
  2. #
  3. # Template::Plugin::HTML
  4. #
  5. # DESCRIPTION
  6. #
  7. #   Template Toolkit plugin providing useful functionality for generating
  8. #   HTML.
  9. #
  10. # AUTHOR
  11. #   Andy Wardley   <abw@kfs.org>
  12. #
  13. # COPYRIGHT
  14. #   Copyright (C) 1996-2001 Andy Wardley.  All Rights Reserved.
  15. #   Copyright (C) 1998-2001 Canon Research Centre Europe Ltd.
  16. #
  17. #   This module is free software; you can redistribute it and/or
  18. #   modify it under the same terms as Perl itself.
  19. #
  20. #----------------------------------------------------------------------------
  21. #
  22. # $Id: HTML.pm,v 2.56 2004/01/13 16:20:38 abw Exp $
  23. #
  24. #============================================================================
  25.  
  26. package Template::Plugin::HTML;
  27.  
  28. require 5.004;
  29.  
  30. use strict;
  31. use vars qw( $VERSION );
  32. use base qw( Template::Plugin );
  33. use Template::Plugin;
  34.  
  35. $VERSION = sprintf("%d.%02d", q$Revision: 2.56 $ =~ /(\d+)\.(\d+)/);
  36.  
  37. sub new {
  38.     my ($class, $context, @args) = @_;
  39.     my $hash = ref $args[-1] eq 'HASH' ? pop @args : { };
  40.     bless {
  41.     _SORTED => $hash->{ sorted } || 0,
  42.     }, $class;
  43. }
  44.  
  45. sub element {
  46.     my ($self, $name, $attr) = @_;
  47.     ($name, $attr) = %$name if ref $name eq 'HASH';
  48.     return '' unless defined $name and length $name;
  49.     $attr = $self->attributes($attr);
  50.     $attr = " $attr" if $attr;
  51.     return "<$name$attr>";
  52. }
  53.  
  54. sub attributes {
  55.     my ($self, $hash) = @_;
  56.     return '' unless UNIVERSAL::isa($hash, 'HASH');
  57.  
  58.     my @keys = keys %$hash;
  59.     @keys = sort @keys if $self->{ _SORTED };
  60.  
  61.     join(' ', map { 
  62.     "$_=\"" . $self->escape( $hash->{ $_ } ) . '"';
  63.     } @keys);
  64. }
  65.  
  66. sub escape {
  67.     my ($self, $text) = @_;
  68.     for ($text) {
  69.     s/&/&/g;
  70.     s/</</g;
  71.     s/>/>/g;
  72.     s/"/"/g;
  73.     }
  74.     $text;
  75. }
  76.  
  77. sub url {
  78.     my ($self, $text) = @_;
  79.     return undef unless defined $text;
  80.     $text =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
  81.     return $text;
  82. }
  83.  
  84.  
  85. 1;
  86.  
  87. __END__
  88.  
  89.  
  90. #------------------------------------------------------------------------
  91. # IMPORTANT NOTE
  92. #   This documentation is generated automatically from source
  93. #   templates.  Any changes you make here may be lost.
  94. #   The 'docsrc' documentation source bundle is available for download
  95. #   from http://www.template-toolkit.org/docs.html and contains all
  96. #   the source templates, XML files, scripts, etc., from which the
  97. #   documentation for the Template Toolkit is built.
  98. #------------------------------------------------------------------------
  99.  
  100. =head1 NAME
  101.  
  102. Template::Plugin::HTML - Plugin to create HTML elements
  103.  
  104. =head1 SYNOPSIS
  105.  
  106.     [% USE HTML %]
  107.  
  108.     [% HTML.escape("if (a < b && c > d) ..." %]
  109.  
  110.     [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
  111.  
  112.     [% HTML.attributes(border => 1, cellpadding => 2) %]
  113.  
  114. =head1 DESCRIPTION
  115.  
  116. The HTML plugin is very new and very basic, implementing a few useful
  117. methods for generating HTML.  It is likely to be extended in the future
  118. or integrated with a larger project to generate HTML elements in a generic
  119. way (as discussed recently on the mod_perl mailing list).
  120.  
  121. =head1 METHODS
  122.  
  123. =head2 escape(text)
  124.  
  125. Returns the source text with any HTML reserved characters such as 
  126. E<lt>, E<gt>, etc., correctly esacped to their entity equivalents.
  127.  
  128. =head2 attributes(hash)
  129.  
  130. Returns the elements of the hash array passed by reference correctly
  131. formatted (e.g. values quoted and correctly escaped) as attributes for
  132. an HTML element.
  133.  
  134. =head2 element(type, attributes)
  135.  
  136. Generates an HTML element of the specified type and with the attributes
  137. provided as an optional hash array reference as the second argument or
  138. as named arguments.
  139.  
  140.     [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
  141.     [% HTML.element('table', border=1, cellpadding=2) %]
  142.     [% HTML.element(table => attribs) %]
  143.  
  144. =head1 DEBUGGING
  145.  
  146. The HTML plugin accepts a 'sorted' option as a constructor argument
  147. which, when set to any true value, causes the attributes generated by
  148. the attributes() method (either directly or via element()) to be
  149. returned in sorted order.  Order of attributes isn't important in
  150. HTML, but this is provided mainly for the purposes of debugging where
  151. it is useful to have attributes generated in a deterministic order
  152. rather than whatever order the hash happened to feel like returning
  153. the keys in.
  154.  
  155.     [% USE HTML(sorted=1) %]
  156.     [% HTML.element( foo => { charlie => 1, bravo => 2, alpha => 3 } ) %]
  157.  
  158. generates:
  159.  
  160.     <foo alpha="3" bravo="2" charlie="1">
  161.  
  162. =head1 AUTHOR
  163.  
  164. Andy Wardley E<lt>abw@andywardley.comE<gt>
  165.  
  166. L<http://www.andywardley.com/|http://www.andywardley.com/>
  167.  
  168.  
  169.  
  170.  
  171. =head1 VERSION
  172.  
  173. 2.56, distributed as part of the
  174. Template Toolkit version 2.13, released on 30 January 2004.
  175.  
  176. =head1 COPYRIGHT
  177.  
  178.   Copyright (C) 1996-2004 Andy Wardley.  All Rights Reserved.
  179.   Copyright (C) 1998-2002 Canon Research Centre Europe Ltd.
  180.  
  181. This module is free software; you can redistribute it and/or
  182. modify it under the same terms as Perl itself.
  183.  
  184. =head1 SEE ALSO
  185.  
  186. L<Template::Plugin|Template::Plugin>
  187.  
  188. =cut
  189.  
  190. # Local Variables:
  191. # mode: perl
  192. # perl-indent-level: 4
  193. # indent-tabs-mode: nil
  194. # End:
  195. #
  196. # vim: expandtab shiftwidth=4:
  197.