| HTML-Template-Compiled documentation | Contained in the HTML-Template-Compiled distribution. |
HTML::Template::Compiled::Plugin::XMLEscape - XML-Escaping for HTC
use HTML::Template::Compiled::Plugin::XMLEscape;
my $htc = HTML::Template::Compiled->new(
plugin => [qw(HTML::Template::Compiled::Plugin::XMLEscape)],
...
);
gets called by HTC
escapes data for XML CDATA.
escapes data for XML attributes
use HTML::Template::Compiled::Plugin::XMLEscape;
my $htc = HTML::Template::Compiled->new(
plugin => [qw(HTML::Template::Compiled::Plugin::XMLEscape)],
tagstyle => [qw(-classic -comment -asp +tt)],
scalarref => \'<foo attr="[%= attribute %]">[%= cdata escape=XML %]</foo>',
default_escape => 'XML_ATTR',
);
$htc->param(
attr => 'foo & bar',
cdata => 'text < with > tags',
);
print $htc->output;
Output:
<foo attr="foo & bar">text < with > tags</foo>
| HTML-Template-Compiled documentation | Contained in the HTML-Template-Compiled distribution. |
package HTML::Template::Compiled::Plugin::XMLEscape; # $Id: XMLEscape.pm 969 2007-09-10 20:08:42Z tinita $ use strict; use warnings; use Carp qw(croak carp); use HTML::Template::Compiled; HTML::Template::Compiled->register(__PACKAGE__); our $VERSION = '0.03'; sub register { my ($class) = @_; my %plugs = ( escape => { # <tmpl_var foo escape=XML> XML => \&escape_xml, XML_ATTR => 'HTML::Template::Compiled::Plugin::XMLEscape::escape_xml_attr', }, ); return \%plugs; } sub escape_xml { defined( my $escaped = $_[0] ) or return; $escaped =~ s/&/&/g; $escaped =~ s/</</g; $escaped =~ s/>/>/g; $escaped =~ s/"/"/g; $escaped =~ s/'/'/g; return $escaped; } sub escape_xml_attr { defined( my $escaped = $_[0] ) or return; $escaped =~ s/&/&/g; $escaped =~ s/</</g; $escaped =~ s/>/>/g; $escaped =~ s/"/"/g; $escaped =~ s/'/'/g; return $escaped; } 1; __END__