Hi,

If you are, like me, writing your HTML templates using a state of the
art (i.e., unicode aware) editor but still want your non-ascii
characters to produce HTML &entities; to avoid poratbility problems,
this may be of help. It is a filter that translates all non-ASCII into
HTML &entities;. It's just like the builtin 'html' filter but leaves
<, >, &, and &quote; unmodified.

I use it in my page/wrapper :

    [%

    USE UTF8ToHTML;             # for utf8tohtml filter

    SWITCH page.type;
        CASE "text";
            content;

        CASE "html";
            content | utf8tohtml WRAPPER page/html
                          + page/layout

        ...

        CASE;
            THROW page.type "Invalid page type: $page.type";
    END;

    -%]

Here's the fiter:

----snip----
#! perl

use strict;
use warnings;

package Template::Plugin::UTF8ToHTML;

use base qw( Template::Plugin::Filter );

use HTML::Entities;

sub init {
    my $self = shift;
    my $name = $self->{ _CONFIG }->{name} || 'utf8tohtml';
    $self->install_filter($name);
    return $self;
}

sub filter {
    my ( $self, $parameter ) = @_;
    encode_entities( $parameter, '^\n\x20-\x7e' );
    $parameter;
}

1;
----snip----

Happy hacking!

-- Johan

_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to