AW: [Templates] TT and PHP + Attachement

[prev] [thread] [next] [Date index for 2005/02/25]

From: Denis Banovic
Subject: AW: [Templates] TT and PHP + Attachement
Date: 14:55 on 25 Feb 2005
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C51B4A.146C722B
Content-Type: text/plain;
		charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Sorry, I forgot the attached file

-----Urspr=FCngliche Nachricht-----
Von: Denis Banovic=20
Gesendet: Freitag, 25. Februar 2005 15:54
An: 'Andy Wardley'
Cc: 'templates@xxxxxxxxxxxxxxxx.xxx'
Betreff: AW: [Templates] TT and PHP


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

------_=_NextPart_001_01C51B4A.146C722B
Content-Type: application/octet-stream;
		name="PHP.pm"
Content-Transfer-Encoding: base64
Content-Description: PHP.pm
Content-Disposition: attachment;
		filename="PHP.pm"

cGFja2FnZSBUZW1wbGF0ZTo6RGlyZWN0aXZlOjpQSFA7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AgfQoKICAgICRvdXRwdXQgLj0gcGhw
X3RhZygnfScpOwoKICAgIHJldHVybiAkb3V0cHV0Owp9CgojLS0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tCiMgZm9y
ZWFjaCgkdGFyZ2V0LCAkbGlzdCwgJGFyZ3MsICRibG9jaykKIy0tLS0tLS0tLS0tLS0tLS0tLS0t
LS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLQoKc3Vi
IGZvcmVhY2ggewogICAgbXkgKCRjbGFzcywgJHRhcmdldCwgJGxpc3QsICRhcmdzLCAkYmxvY2sp
ID0gQF87CiAgICBteSAkb3V0cHV0ID0gcGhwX2Jsb2NrKCJmb3JlYWNoICgkbGlzdCBhcyBcJCR0
YXJnZXQpIHsiLCAkYmxvY2spOwogICAgJG91dHB1dCAuPSBwaHBfdGFnKCd9Jyk7CiAgICByZXR1
cm4gJG91dHB1dDsKfQoKMTsKCl9fRU5EX18KCg==

------_=_NextPart_001_01C51B4A.146C722B--


_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

AW: [Templates] TT and PHP + Attachement
Denis Banovic 14:55 on 25 Feb 2005

Re: AW: [Templates] TT and PHP + Attachement
Simon Wilcox 15:48 on 25 Feb 2005

Re: AW: [Templates] TT and PHP + Attachement
merlyn (Randal L. Schwartz) 19:24 on 25 Feb 2005

Generated at 08:55 on 15 Mar 2005 by mariachi v0.52