NYCPHP Meetup

NYPHP.org

Looking for a PHP equivalent to Perl Text::Template

Emmanuel. M. Decarie emm at scriptdigital.com
Mon May 5 17:45:44 EDT 2003


Hello,

I'm looking to a PHP equivalent to the Perl module Text::Template.

I was perusing the Smarty documentation, but, maybe I missed 
something, but it doesn't look that I can use  it the in same way.

What I want to do is to assign the result of parsing the template to 
a variable. This is very convenient for example when you need to send 
email.

---------------------------------------------
Perl example:
---------------------------------------------

Dear {$title} {$lastname},
It has come to our attention that you are delinquent in your
         {$monthname[$last_paid_month]} payment.  Please remit
         ${sprintf("%.2f", $amount)} immediately, or your patellae may
         be needlessly endangered.
Love,
Mark "Vizopteryx" Dominus

---------------------------------------------
Perl code:
---------------------------------------------

use Text::Template;
my $template = Text::Template->new(SOURCE => 'formletter.tmpl');
my @monthname = qw(January February March April May June
                            July August September October November December);
         my %vars = (title => 'Mr.',
                     firstname => 'Bill',
                     lastname => 'Gates',
                     last_paid_month => 1,   # February
                     amount => 392.12,
                     monthname => \\@monthname,
                    );
my $result = $template->fill_in(HASH => \\%vars);
<http://perl.plover.com/Template/Manual.html#example>
---------------------------------------------

Now, I know that I could write my own parser based on regex, but if 
something exists like this in PHP, I rather use it.

I know that I can use an include like this (or built on this and use 
a "here document"):

<?php

$var = 'PHP';

return $var;

?>
<http://www.php.net/manual/en/function.include.php>

But its just not convenient for non programmers because they will 
need to escape the quotes that hold the string.

So, is there like a beast like the Perl Text::Template in the PHP world?

TIA
-- 
______________________________________________________________________
Emmanuel Décarie / Programmation pour le Web - Programming for the Web
Radio UserLand/Frontier - Perl - PHP - Javascript  <http://scriptdigital.com/>
Blog: <http://blog.scriptdigital.com> - AIM: scriptdigital




More information about the talk mailing list