AW: [Templates] TT and PHP
[prev]
[thread]
[next]
[Date index for 2005/02/25]
Hi Andy,
First of all I wanted to thank you once again for building TT, I'm looking f=
orward to see the TT3 in action.
We've met in San Diego on the OSCON 2001 or 2002 I think.
Thank you for your help but I still feel a bit misunderstood.
I think that I've put this question on the wrong list. It's true that I need=
to convert TT-Templates to PHP, but I must do it IN PHP.
And as most of us ont this list are more on the PERL than PHP, I think I sho=
uld ask this question on some PHP list. But I think they won't understand me=
either, because most of them are not using any templating system at all...
I think I'll have to do some PHP-Regexing hack by myself. I just though some=
one has done something like this before.
Thanks
Denis
-----Urspr=FCngliche Nachricht-----
Von: Andy Wardley [mailto:abw@xxxxxxx.xxx]=20
Gesendet: Freitag, 25. Februar 2005 14:30
An: Denis Banovic
Betreff: Re: [Templates] TT and PHP
Denis Banovic wrote:
> Yes, that's correct, I'm looking for a way to convert (some parts of )=20
> TT ( eg. [ FOREACH and IF ] ) to PHP. Our PHP templates are very similar=20
> to TT-Templates,=20
One approach is to subclass Template::Directive (the part of TT that
generates compiled Perl code from the source template) and intercept
the various methods that generate the foreach, if, and other directives.
In the usual case, the methods generate Perl code to implements the foreach,=20
if, etc. e.g.=20
# template # regular compiled Perl code
[% IF x %]... =3D> if ($stash->get('x')) { ...
You would instead need to generate Perl code that when run, emits the PHP=20
equivalent of the original directive.
# template # output PHP directive
[% IF x %]... =3D> $output .=3D '<? if ($x) { ?>...';
The end result is that you process your TT templates, and you get PHP
templates generated as the output.
There is a Template::Directive::PHP module attached which I just knocked
up to test this idea. Be warned that it is a proof of concept only and
will almost certainly break when you feed it other TT directives. However
it may be useful as a starting point if you decide that's a good way to go.
The difficulty is that in TT2 there isn't a clear separation between the
parser and back end directive generator (yet another weakness that I've=20
been trying to address in TT3). You need to become intimately familiar=20
with the workings and output generated by both the parser and current=20
Template::Directives methods, and it's not something to be taken lightly. =20
I suspect there may be several things that are hard, if not impossible to=20
do correctly without hacking on the parser grammar itself, and therein=20
lies dragons! =20
So if you're not already totally confused by the examples above, you soon
will be once you start delving deeper :-)
Anyway, here's an example of my quick hack in use:
use strict;
use warnings;
use Template;
use Template::Parser;
use Template::Directive::PHP;
# -d flag enables debugging output
if (grep(/^-d/, @ARGV)) {
$Template::Parser::DEBUG =3D 1;
$Template::Directive::PRETTY =3D 1;
}
my $tt =3D Template->new( FACTORY =3D> 'Template::Directive::PHP' )
|| die Template->error();
$tt->process(\*DATA)=20
|| die $tt->error();
__DATA__
This is a test template
[% IF x < 10 %]
do this
[% ELSIF y < 20 %]
do this instead
[% ELSE %]
don't do anything at all
[% END %]
=20
[% z %]
[% FOREACH y IN z %]
y is [% y %]
[% END %]
And this is the output:
This is a test template
<? if ($x < 10) { ?>
do this
<? } elsif ($y < 20) { ?>
do this instead
<? } else { ?>
don't do anything at all
<? } ?>
=20
<? $z ?>
=20
<? foreach ($z as $y) { ?>
y is <? $y ?>
<? } ?>
Cheers
A
____________
Virus checked by G DATA AntiVirusKit
Version: AVK 15.0.2702 from 26.01.2005
Virus news: www.antiviruslab.com
_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates
 |
(message missing)
|
 |
 |
AW: [Templates] TT and PHP
Denis Banovic 14:53 on 25 Feb 2005
|