I copied the parse routine from the Enhanced Template mod (which has terrible formatting, by the way-- hopefully it doesn't affect the script), but after doing so my parsed scripts just come up as blank pages. Restoring the old Template.pm returns the scripts to working order.
I checked my cut-and-paste job and it's correct, as are the file permissions. So my guess is there's something wrong with the script as posted in the Modifications section. Here's a copy of what I've got, with formatting fixed. I'd appreciate any help in getting this fixed, as I need to include files in my templates.
Thanks!
- Peter
------
# ---------------------------------------------------------------
# Parses a template.
#
my ($self, $template) = @_;
my $begin = $self->{'begin'} || quotemeta('<%');
my $end = $self->{'end'} || quotemeta('%>');
my $root = $self->{'ROOT'};
exists $self->{'templates'}{$root}{$template} or ($self->load_template($template) or return undef);
exists $self->{'vars'} or (&error ('NONAMESPACE') and return undef);
$self->{'parsed'}{$template} = '';
my $temp = $self->{'templates'}{$root}{$template};
# Parse includes, do this first so that the includes can include
# template tags.
$temp =~ s#$begin\s*include\s*(.+?)\s*$end#
if (exists $self->{'inc'}{$root}{$1}) { $self->{'inc'}{$root}{$1}; }
else {
if (open (INC, "${$self}{'ROOT'}/$1"))
{
$self->{'inc'}{$root}{$1} = join ("", <INC> );
close INC;
$self->{'inc'}{$root}{$1};
}
else
{
"Can't find file: ${$self}{'ROOT'}/$1";
}
}
#goe;
# Now go line by line and strip out the unwanted stuff looking for
# if and ifnot tags.
my @lines = split /\n/, $temp;
$temp = ''; my @go = (1,1); my $depth = 1; my $line = '';
LINE: foreach $line (@lines)
{
# Init the previous, variable and more strings.
my ($prev, $var, $neg, $more, $orig, $full_comp, $comp, $val) = ('', '', '', '', '', '', '', '');
my $result = 0;
# Check for if tags.
$line =~ s/((.*?)$begin\s*if(not)?\s+(.+?)(\s+(<|>|lt|gt|eq|==?)\s*(.+?))?$end(.*))/
($orig, $prev, $neg, $var, $full_comp, $comp, $val, $more) = ($1, $2, $3, $4, $5, $6, $7, $8);
# We've found an if tag, let's set the depth to see whether we are in print mode or not.
if ($prev !~ m,$begin\s*endif\s*$end,og) {
$go[$depth] and ($temp .= $prev);
if (!$full_comp)
{
if ($neg)
{
($self->{'vars'}{$var}) ? ($go[++$depth] = 0) : ($go[++$depth] = $go[$depth]) and "";
}
else {
($self->{'vars'}{$var}) ? ($go[++$depth] = $go[$depth]) : ($go[++$depth] = 0) and "";
}
}
else
{
$val =~ s,^['"],,; $val =~ s,['"]$,,;
($comp eq 'eq') and ($result = ($self->{'vars'}{$var} eq $val));
($comp eq '=') and ($result = ($self->{'vars'}{$var} == $val));
($comp eq '==') and ($result = ($self->{'vars'}{$var} == $val));
($comp eq 'lt') and ($result = ($self->{'vars'}{$var} lt $val));
($comp eq 'gt') and ($result = ($self->{'vars'}{$var} gt $val));
($comp eq '>') and ($result = ($self->{'vars'}{$var} > $val));
($comp eq '<') and ($result = ($self->{'vars'}{$var} < $val));
if ($neg) {
$result ? ($go[++$depth] = 0) : ($go[++$depth] = $go[$depth]) and "";
}
else
{
$result ? ($go[++$depth] = $go[$depth]) : ($go[++$depth] = 0) and "";
}
}
}
else
{
# Oops, there was an endif tag we missed, set the original line back and keep going.
$more = '';
$orig;
}
/oe;
if ($more) {
$line = $more;
redo LINE;
}
# Check for endif tags.
$line =~ s/(.*?)$begin\s*endif\s*$end(.*)/
($prev, $more) = ($1, $2);
$go[$depth] and ($temp .= $prev);
$go[$depth--] = 1;
"";
/oe;
if ($more)
{
$line = $more;
redo LINE;
}
# Add the content..
$go[$depth] and ($temp .= "$line\n");
}
# Replace the special variables, we allow code ref mapping.
$temp =~ s/$begin\s*(.+?)\s*$end/
if (exists $self->{'vars'}{$1})
{
ref ($self->{'vars'}{$1}) eq 'CODE' ?
&{$self->{'vars'}{$1}}($self->{'vars'}) : $self->{'vars'}{$1};
}
else
{ "Unkown Tag: $1"; }
/goe;
$self->{'parsed'}{$template} = $temp;
return $self->{'parsed'}{$template};
}
I checked my cut-and-paste job and it's correct, as are the file permissions. So my guess is there's something wrong with the script as posted in the Modifications section. Here's a copy of what I've got, with formatting fixed. I'd appreciate any help in getting this fixed, as I need to include files in my templates.
Thanks!
- Peter
------
Code:
sub parse { # ---------------------------------------------------------------
# Parses a template.
#
my ($self, $template) = @_;
my $begin = $self->{'begin'} || quotemeta('<%');
my $end = $self->{'end'} || quotemeta('%>');
my $root = $self->{'ROOT'};
exists $self->{'templates'}{$root}{$template} or ($self->load_template($template) or return undef);
exists $self->{'vars'} or (&error ('NONAMESPACE') and return undef);
$self->{'parsed'}{$template} = '';
my $temp = $self->{'templates'}{$root}{$template};
# Parse includes, do this first so that the includes can include
# template tags.
$temp =~ s#$begin\s*include\s*(.+?)\s*$end#
if (exists $self->{'inc'}{$root}{$1}) { $self->{'inc'}{$root}{$1}; }
else {
if (open (INC, "${$self}{'ROOT'}/$1"))
{
$self->{'inc'}{$root}{$1} = join ("", <INC> );
close INC;
$self->{'inc'}{$root}{$1};
}
else
{
"Can't find file: ${$self}{'ROOT'}/$1";
}
}
#goe;
# Now go line by line and strip out the unwanted stuff looking for
# if and ifnot tags.
my @lines = split /\n/, $temp;
$temp = ''; my @go = (1,1); my $depth = 1; my $line = '';
LINE: foreach $line (@lines)
{
# Init the previous, variable and more strings.
my ($prev, $var, $neg, $more, $orig, $full_comp, $comp, $val) = ('', '', '', '', '', '', '', '');
my $result = 0;
# Check for if tags.
$line =~ s/((.*?)$begin\s*if(not)?\s+(.+?)(\s+(<|>|lt|gt|eq|==?)\s*(.+?))?$end(.*))/
($orig, $prev, $neg, $var, $full_comp, $comp, $val, $more) = ($1, $2, $3, $4, $5, $6, $7, $8);
# We've found an if tag, let's set the depth to see whether we are in print mode or not.
if ($prev !~ m,$begin\s*endif\s*$end,og) {
$go[$depth] and ($temp .= $prev);
if (!$full_comp)
{
if ($neg)
{
($self->{'vars'}{$var}) ? ($go[++$depth] = 0) : ($go[++$depth] = $go[$depth]) and "";
}
else {
($self->{'vars'}{$var}) ? ($go[++$depth] = $go[$depth]) : ($go[++$depth] = 0) and "";
}
}
else
{
$val =~ s,^['"],,; $val =~ s,['"]$,,;
($comp eq 'eq') and ($result = ($self->{'vars'}{$var} eq $val));
($comp eq '=') and ($result = ($self->{'vars'}{$var} == $val));
($comp eq '==') and ($result = ($self->{'vars'}{$var} == $val));
($comp eq 'lt') and ($result = ($self->{'vars'}{$var} lt $val));
($comp eq 'gt') and ($result = ($self->{'vars'}{$var} gt $val));
($comp eq '>') and ($result = ($self->{'vars'}{$var} > $val));
($comp eq '<') and ($result = ($self->{'vars'}{$var} < $val));
if ($neg) {
$result ? ($go[++$depth] = 0) : ($go[++$depth] = $go[$depth]) and "";
}
else
{
$result ? ($go[++$depth] = $go[$depth]) : ($go[++$depth] = 0) and "";
}
}
}
else
{
# Oops, there was an endif tag we missed, set the original line back and keep going.
$more = '';
$orig;
}
/oe;
if ($more) {
$line = $more;
redo LINE;
}
# Check for endif tags.
$line =~ s/(.*?)$begin\s*endif\s*$end(.*)/
($prev, $more) = ($1, $2);
$go[$depth] and ($temp .= $prev);
$go[$depth--] = 1;
"";
/oe;
if ($more)
{
$line = $more;
redo LINE;
}
# Add the content..
$go[$depth] and ($temp .= "$line\n");
}
# Replace the special variables, we allow code ref mapping.
$temp =~ s/$begin\s*(.+?)\s*$end/
if (exists $self->{'vars'}{$1})
{
ref ($self->{'vars'}{$1}) eq 'CODE' ?
&{$self->{'vars'}{$1}}($self->{'vars'}) : $self->{'vars'}{$1};
}
else
{ "Unkown Tag: $1"; }
/goe;
$self->{'parsed'}{$template} = $temp;
return $self->{'parsed'}{$template};
}