On Fri, Feb 07, 2003 at 12:54:59PM -0500, Sam Tregar wrote:
> On Fri, 7 Feb 2003, Drew Taylor wrote:
> 
> > Is there a way to trim the extra whitespace\newlines from the final output?
> > I didn't notice anything in the docs so I thought I'd ask here.
> 
> HTML::Clean works for some people.  In general I don't let it bother me.
> It's just a few extra bytes and the browser certainly doesn't care.

Problem is, sometimes the browser -does- care. There's not a difference
between "one character of whitespace" and "many characters of whitespace", but
there is a difference between those and "zero characters of whitespace". 

Me personally, I use an HTML::Template::Extension module to do it. Heck, 
said module even appears to be appended to this email. Usage: use PACKTAGS
as an extension, and close some of your tags with " #>" (note the space)
instead of >. Any tags closed that way will cut off output until the next
tag in the template. (This could be trivially adjusted to just remove 
whitespace, as well .. adjust _pack_tags in the obvious way).

Incidentally, this script solves one of the two complaints I've had about
HTML::Template. The other complaint? There isn't a way to incrementally
render loops, since you have to give it the entire contents of the loop at
once. It seems silly to hold several thousand iterations of data in memory, 
when one could just output an iteration at a time...

Anyhoo. Enjoy.

-jay

                           ----- CUT HERE -----


package HTML::Template::Extension::PACKTAGS;

$VERSION            = "0.01";
sub Version         { $VERSION; }

use Carp;
use strict;

my $classname;
my $parentname;


my %fields  =
(
 ecp_compatibility_mode => 0,
);
     

my @fields_req  = qw//;


sub new
{
    $classname = shift;
    my $self = shift;
    $parentname = ref($self);
    bless $self,$classname;

    $self->_init_local(@_);
    return $self;
}


sub _init_local {
    my $self = shift;
    my (%options) = @_;

    # Assign default options
    while (my ($key,$value) = each(%fields)) {
        $self->{$key} = $self->{$key} || $value;
    }

    # Assign options
    while (my ($key,$value) = each(%options)) {
        $self->{$key} = $value
    }

    # Check required params
    foreach (@fields_req) {
        croak "You must declare '$_' in " . ref($self) . "::new"
        if (!defined $self->{$_});
    }

    $self->push_filter();
}


sub push_filter {
    my $self = shift;
    bless($self, $classname);

    push(@{$self->{filter}}, @{$self->_get_filter()});
    bless($self, $parentname);
}


sub _get_filter {
    my $self = shift;
    my @ret ;

    # Sorry for this :->. I've an e-commerce project called ecp that
    # use a CSTART modified syntax using html comment
    push @ret,\&_pack_tags if ($self->{ecp_compatibility_mode});

    # Standard CSTART syntax
    push @ret,\&_pack_tags;

    return \@ret;
}


sub _pack_tags {
    my $template = shift;

#    $template = $$template;
#    print STDERR $template;

    $$template =~ s/\s+#>[^<]*</></g;

    return $template;
}

1;


-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
Html-template-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/html-template-users

Reply via email to