Sorry, I forgot the attached file -----Urspr�ngliche Nachricht----- Von: Denis Banovic Gesendet: Freitag, 25. Februar 2005 15:54 An: 'Andy Wardley' Cc: '[email protected]' Betreff: AW: [Templates] TT and PHP
Hi Andy, First of all I wanted to thank you once again for building TT, I'm looking forward 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 should 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 someone has done something like this before. Thanks Denis -----Urspr�ngliche Nachricht----- Von: Andy Wardley [mailto:[EMAIL PROTECTED] 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 ) > TT ( eg. [ FOREACH and IF ] ) to PHP. Our PHP templates are very similar > to TT-Templates, 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, if, etc. e.g. # template # regular compiled Perl code [% IF x %]... => if ($stash->get('x')) { ... You would instead need to generate Perl code that when run, emits the PHP equivalent of the original directive. # template # output PHP directive [% IF x %]... => $output .= '<? 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 been trying to address in TT3). You need to become intimately familiar with the workings and output generated by both the parser and current Template::Directives methods, and it's not something to be taken lightly. I suspect there may be several things that are hard, if not impossible to do correctly without hacking on the parser grammar itself, and therein lies dragons! 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 = 1; $Template::Directive::PRETTY = 1; } my $tt = Template->new( FACTORY => 'Template::Directive::PHP' ) || die Template->error(); $tt->process(\*DATA) || 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 %] [% 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 <? } ?> <? $z ?> <? 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
PHP.pm
Description: PHP.pm
