Templates - Perl to PHP
[email protected] (Marco Aurélio) Tue, 6 Mar 2001 22:20:53 -0300
| Newsgroups | php.template |
|---|---|
| Message-ID | <006d01c0a6a4$dd8b11a0$0100a8c0@marcoa> |
------=_NextPart_000_006A_01C0A68B.B57543C0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: 8bit
Hello!
Do you know Links 2.0 by Gossamer-Threads ?
Well, this perl script uses a template engine(annexed file) based in a
modification of CGI::FastTemplate that give a useful flexibility for
webdesigners. The facilities are that webdesigners can use logical functions
in his tags on templates (actually if, if not, >, <, = =, != ) eg:
{IF IMAGE}
<p>
<img src="{IMAGE}">
<br>
<br>
{ENDIF}
Thus all code between the if statement will be printed if $image is set else
nothing will be.
This is very useful when you have a list of elements that someone have some
elements and others not, eg:
1) The template without this enhancement:
<p>
<img src="{IMAGE}"><br><br>
{NAME}<br>
{EMAIL}
Thus if the current user doesn't have a image the <p>, <img> and <br><br>
tags will be printed in any way.
2) The template with the enhancement:
{IF IMAGE}
<p>
<img src="{IMAGE}">
<br>
<br>
{ENDIF}
{NAME}<br>
{EMAIL}
Thus if the current user doesn't have a image the <p>, <img> and <br><br>
tags will not be printed in any way, maintaining the desired design. THIS IS
GREAT !!!
Note)
Also, would be excellent if we have the following options on tags:
1) include a file not parsed (straight include of HTML) (allows php code)
2) include a file parsed (parse the include file for tags) (allows php code)
Is possible to convert the Template.pm (see annexed file) by Gossamer
to php class ???
Kindest Regards,
Marco Aurélio
Brazil
[email protected]
BTW(a), I tried dozen based FastTemplates engines but anyone
have the functions of Template.pm.
BTW(b):
1) Can you understand me ??? (sorry for my poor english.)
2) If have any cost, please tell me.
3) Please answer me, I really need it.
------=_NextPart_000_006A_01C0A68B.B57543C0
Content-Type: application/octet-stream;
name="Template.pm"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
filename="Template.pm"
#-------------
#Links
#-------------
#Links Manager
#
#File: Template.pm=20
# Description: Template parser package lossely based off of =
CGI::FastTemplate
# Author: Alex Krohn
# Email: [email protected]
# Web: http://www.gossamer-threads.com/
# Version: 2.0
#
# (c) 1998 Gossamer Threads Inc.=20
#
# This script is not freeware! Please read the README for full details
# on registration and terms of use.=20
# =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
package Template;
# =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
use strict;
use vars qw($VERSION $error @ISA @EXPORT); # Globals.
@ISA =3D (); # Not inhereited.
@EXPORT =3D qw();
$Template::VERSION =3D '1.0';
sub error {
# ---------------------------------------------------------------
# Handles error messages.
#
my ($errtype, $e1, $e2) =3D @_;
my %errors =3D (
'CANTINIT' =3D> "Template: Can't init, no root =
defined, or not a hash ref: '$e1'",
'NOTEMPLATE' =3D> "Template: Can't find template: =
$e1",
'CANTOPEN' =3D> "Template: Can't open template: =
$e1. Reason: $e2",
'NONAMESPACE' =3D> "Tempalte: You haven't loaded a =
namespace yet!",
'NONAME' =3D> "Template: No name was defined =
for this template!",
'NOSTRING' =3D> "Template: No text was defined =
for template: $e1"
);
$Template::error =3D $errors{$errtype};
die $Template::error;
}
sub new {
# ---------------------------------------------------------------
# Initilizes a new instance of the template parser.
#
my $this =3D shift;
my $class =3D ref($this) || $this;
my $self =3D {};
bless $self, $class; =20
=20
my $opt =3D shift;
unless ((ref $opt eq 'HASH') and (exists ${$opt}{'ROOT'})) {
&error ('CANTINIT', $opt);
return undef;
}
$self->{'ROOT'} =3D ${$opt}{'ROOT'};
$self->{'CHECK'} =3D ${$opt}{'CHECK'};
return $self;
}
sub load_template {
# ---------------------------------------------------------------
# Loads a template either from a file or from a string.
#
my $self =3D shift;
my ($name, $text) =3D @_;
if (!$self->{'CHECK'} and exists $self->{'templates'}{$name}) { =
return 1; }=20
if (!defined $text) {
my $file =3D $self->{'ROOT'} . "/$name";
-e $file or (&error('NOTEMPLATE', $file) and =
return undef);
open (TPL, "<$file") or (&error('CANTOPEN', $file, $!) and =
return undef);
$text =3D join ("", <TPL>);
close TPL;
}
$self->{'templates'}{$name} =3D $text;
return 1;
}
sub load_vars {
# ---------------------------------------------------------------
# Sets the variables (all the search and replaces).
#
my $self =3D shift;
my $vars =3D shift;
my ($name, $value);
while (($name, $value) =3D each %{$vars}) { =20
$self->{'vars'}{$name} =3D $value;
}
return 1; =20
}
sub clear_vars {
# ---------------------------------------------------------------
# Clears the namespace.
#
my $self =3D shift;
$self->{'vars'} =3D {};
return 1;
}
=20
sub parse {
# ---------------------------------------------------------------
# Parses a template.
#
my ($self, $template) =3D @_;
my $begin =3D $self->{'begin'} || quotemeta('<%');
my $end =3D $self->{'end'} || quotemeta('%>');
exists $self->{'templates'}{$template} or =
($self->load_template($template) or return undef);
exists $self->{'vars'} or (&error ('NONAMESPACE') and return undef);
$self->{'parsed'}{$template} =3D '';
my $temp =3D $self->{'templates'}{$template};
# Parse includes, do this first so that the includes can include
# template tags.
$temp =3D~ s#$begin\s*include\s*(.+?)\s*$end#
if (exists $self->{'inc'}{$1}) { $self->{'inc'}{$1}; }
else {
if (open (INC, "${$self}{'ROOT'}/$1")) {=20
$self->{'inc'}{$1} =3D join ("", <INC> );
close INC;
$self->{'inc'}{$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 =3D split /\n/, $temp;
$temp =3D ''; my @go =3D (1,1); my $depth =3D 1; my $line =3D =
'';
LINE: foreach $line (@lines) {
# Init the previous, variable and more strings.
my ($prev, $var, $neg, $more, $orig, $full_comp, $comp, $val) =
=3D ('', '', '', '', '', '', '', '');
my $result =3D 0;
# Check for if tags.=20
$line =3D~ =
s/((.*?)$begin\s*if(not)?\s+(.+?)(\s+(<|>|lt|gt|eq|=3D)\s*(.+?))?$end(.*)=
)/
($orig, $prev, $neg, $var, $full_comp, $comp, $val, =
$more) =3D ($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 .=3D $prev);
if (!$full_comp) {
if ($neg) { ($self->{'vars'}{$var}) ? =
($go[++$depth] =3D 0) : ($go[++$depth] =3D $go[$depth]) and ""; }
else { ($self->{'vars'}{$var}) ? ($go[++$depth] =3D =
$go[$depth]) : ($go[++$depth] =3D 0) and ""; }
}
else {
$val =3D~ s,^['"],,; $val =3D~ s,['"]$,,;
($comp eq 'eq') and ($result =3D ($self->{'vars'}{$var} eq =
$val));
($comp eq '=3D~') and ($result =3D ($self->{'vars'}{$var} =3D~ =
$val));
($comp eq '=3D=3D') and ($result =3D ($self->{'vars'}{$var} =
=3D=3D $val));
($comp eq 'lt') and ($result =3D ($self->{'vars'}{$var} lt =
$val));
($comp eq 'gt') and ($result =3D ($self->{'vars'}{$var} gt =
$val));
($comp eq '>') and ($result =3D ($self->{'vars'}{$var} > $val));
($comp eq '<') and ($result =3D ($self->{'vars'}{$var} < $val));
if ($neg) { $result ? ($go[++$depth] =3D 0) : ($go[++$depth] =3D =
$go[$depth]) and ""; }
else { $result ? ($go[++$depth] =3D $go[$depth]) : =
($go[++$depth] =3D 0) and ""; }
}
}
else {
# Oops, there was an endif tag we missed, set the original line back and =
keep going.
$more =3D ''; $orig;
}
/oe;
if ($more) { $line =3D $more; redo LINE; }
# Check for endif tags.
$line =3D~ s/(.*?)$begin\s*endif\s*$end(.*)/
($prev, $more) =3D ($1, $2);
$go[$depth] and ($temp .=3D $prev);
$go[$depth--] =3D 1;
"";
/oe;
if ($more) { $line =3D $more; redo LINE; }
# Add the content..
$go[$depth] and ($temp .=3D "$line\n");
}
# Replace the special variables, we allow code ref mapping.
$temp =3D~ s/$begin\s*(.+?)\s*$end/
if (exists $self->{'vars'}{$1}) {
ref ($self->{'vars'}{$1}) eq 'CODE' ?=20
&{$self->{'vars'}{$1}}($self->{'vars'}) : =
$self->{'vars'}{$1};
}
else { "Unkown Tag: $1"; }
/goe;
$self->{'parsed'}{$template} =3D $temp;
return $self->{'parsed'}{$template};
}
sub DESTROY {
# ---------------------------------------------------------------
}
1;
------=_NextPart_000_006A_01C0A68B.B57543C0--