1 #============================================================= -*-Perl-*-
3 # Template::Plugin::HTML
6 # Template Toolkit plugin providing useful functionality for generating
10 # Andy Wardley <abw@wardley.org>
13 # Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
18 #============================================================================
20 package Template::Plugin::HTML;
24 use base 'Template::Plugin';
29 my ($class, $context, @args) = @_;
30 my $hash = ref $args[-1] eq 'HASH' ? pop @args : { };
32 _SORTED => $hash->{ sorted } || 0,
37 my ($self, $name, $attr) = @_;
38 ($name, $attr) = %$name if ref $name eq 'HASH';
39 return '' unless defined $name and length $name;
40 $attr = $self->attributes($attr);
41 $attr = " $attr" if $attr;
42 return "<$name$attr>";
46 my ($self, $hash) = @_;
47 return '' unless ref $hash eq 'HASH';
49 my @keys = keys %$hash;
50 @keys = sort @keys if $self->{ _SORTED };
53 "$_=\"" . $self->escape( $hash->{ $_ } ) . '"';
58 my ($self, $text) = @_;
69 my ($self, $text) = @_;
70 return undef unless defined $text;
71 $text =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
82 Template::Plugin::HTML - Plugin to create HTML elements
88 [% HTML.escape("if (a < b && c > d) ..." %]
90 [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
92 [% HTML.attributes(border => 1, cellpadding => 2) %]
96 The C<HTML> plugin is a very basic plugin, implementing a few useful
97 methods for generating HTML.
103 Returns the source text with any HTML reserved characters such as
104 C<E<lt>>, C<E<gt>>, etc., correctly esacped to their entity equivalents.
106 =head2 attributes(hash)
108 Returns the elements of the hash array passed by reference correctly
109 formatted (e.g. values quoted and correctly escaped) as attributes for
112 =head2 element(type, attributes)
114 Generates an HTML element of the specified type and with the attributes
115 provided as an optional hash array reference as the second argument or
118 [% HTML.element(table => { border => 1, cellpadding => 2 }) %]
119 [% HTML.element('table', border=1, cellpadding=2) %]
120 [% HTML.element(table => attribs) %]
124 The HTML plugin accepts a C<sorted> option as a constructor argument
125 which, when set to any true value, causes the attributes generated by
126 the C<attributes()> method (either directly or via C<element()>) to be
127 returned in sorted order. Order of attributes isn't important in
128 HTML, but this is provided mainly for the purposes of debugging where
129 it is useful to have attributes generated in a deterministic order
130 rather than whatever order the hash happened to feel like returning
133 [% USE HTML(sorted=1) %]
134 [% HTML.element( foo => { charlie => 1, bravo => 2, alpha => 3 } ) %]
138 <foo alpha="3" bravo="2" charlie="1">
142 Andy Wardley E<lt>abw@wardley.orgE<gt> L<http://wardley.org/>
146 Copyright (C) 1996-2007 Andy Wardley. All Rights Reserved.
148 This module is free software; you can redistribute it and/or
149 modify it under the same terms as Perl itself.
159 # perl-indent-level: 4
160 # indent-tabs-mode: nil
163 # vim: expandtab shiftwidth=4: