NYCPHP Meetup

NYPHP.org

[nycphp-talk] Looking for a PHP equivalent to Perl Text::Template

Carlos A Hoyos cahoyos at us.ibm.com
Tue May 6 12:42:10 EDT 2003







Lets see.
Suppose you have a page like this:

<?php
 ... set variables


?>

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

-------------

Add a ob_start(); at the start of the page, and this at the end
<?
$buffer = ob_get_contents();
ob_end_clean();
?>

now, nothing gets output to the screen and buffer has your content.
This of course is a simple example, should be enough for an idea.

Carlos

Intranet: http://w3.ibm.com/sales/ibmcom
.: PTT tool under the projects menu :.


                                                                                                                                      
                      Chris Snyder                                                                                                    
                      <chris at psydeshow.        To:       NYPHP Talk <talk at nyphp.org>                                                  
                      org>                     cc:                                                                                    
                                               Subject:  Re: [nycphp-talk] Looking for a PHP equivalent to Perl Text::Template         
                      05/06/2003 12:14                                                                                                
                      PM                                                                                                              
                      Please respond to                                                                                               
                      talk                                                                                                            
                                                                                                                                      



Wouldn't you still need to eval() the template in order to substitute
runtime values for the placeholders?
Output buffering would clean up the code I suggested, but I'm not sure
how it would solve the problem of separating logic and design...


Carlos A Hoyos wrote:

>You can use the output buffer functions. Easier than eval, and you can use
>all of the php functions to build the output.
>Look at ob_start() and ob_get_contents()....
>
>this are one of the overlooked treassures in php ; )
>
>Carlos
>
>
>

>
>>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
>>
>>
>>
>>
>>
>You could do essentially this by bringing the template into your
>controlling script and eval()'ing it -- something like:
>
>$template= file_get_contents("/path/to/template.html");
>$command= "\\$output= \\"$template\\";";
>eval($command);
>print $output;
>
>
>You couldn't pull off the sprintf() call in the middle of that, but
>everything else will work. You don't even need the curly brackets.
>
>    chris.
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


--
do you have a photoblog yet?
http://fotola.com  (help beta-test!)




--- Unsubscribe at http://nyphp.org/list/ ---







More information about the talk mailing list