NYCPHP Meetup

NYPHP.org

[nycphp-talk] Smarty Templates

Allen Shaw ashaw at polymerdb.org
Wed Nov 2 14:04:51 EST 2005


csnyder wrote:
>
> PHP makes an excellent template language when used with "short tags".
> Dreamweaver still can't handle loops and conditionals, but it doesn't
> choke on the code either.
> 
> An example of PHP template:
> 
> <html>
> <head>
> <title><?=htmlentities($title)?></title>
> </head>
> <body>
> <p>Today is <?=date('M j, Y')?>.</p>
> <ul>
> <?foreach( $messages AS $message ){?>
>   <li><?=htmlentities($message->subject)?><br />
>        <?=htmlentities($message->body)?>
>   </li>
> <?}?>
> </body>
> </html>
> 
> When working this way you need to write a simple template class that
> will search for the appropriate template to use from some path. Once
> identify the template to use, you just include() it (possibly inside
> an output buffer) and you're done.

Wow, what a great PHP templating method.  I am definately going to have 
to re-evaluate my use of the Smarty system.

I work pretty much all day every day with a system that uses Smarty 
templates.  A few years back (already? geez...), when we wanted to port 
an MS-Access database app to PHP/MySQL, I came up with a sloppy hack to 
handle user permissions, navigation, ad-hoc reporting, etc., through db 
tables, and use Smarty templates to format the output.  The system has 
served us well so far (I'm even speculating that it could be useful to 
other people for other applications).  Although, as Chris points out, 
it's a leaner/meaner system that does the templating natively in PHP, 
we've been quite happy with Smarty all this time.

As for the original question, I, like others here it seems, see the 
"separation of logic and display" as kind of a gradient scale -- any 
display system is going to need logical programming structures to handle 
dynamic data.  But a template system of some sort is just about the only 
way you're going to separate business logic from display logic.

"PHP creep" is not so much much more an issue with native PHP templating 
than it is in Smarty, or other template systems that (even reluctantly) 
allow you to include PHP code in your templates.  The point I think is 
not to worry about language-creep but about function-creep: as long as 
the code within a template exists for the purpose of receiving and 
displaying data, shouldn't that be enough to keep things working cleanly?

Good luck finding the system that works for you, Mark.  There are lots 
of choices. The NYPHP presentations page 
(http://www.nyphp.org/content/presentations/index.php) has links to two 
presentations on the topic, which may help you a little:
http://davidmintz.org/presentations/show.php/QuickForm_and_Smarty (audio 
at http://www.nyphp.org/content/mp3/nyphp072704.mp3)
http://www.nyphp.org/content/presentations/3templates/ (audio at 
http://www.nyphp.org/content/mp3/nyphp092303pt1.mp3)

Whatever you choose, somewhere you're going to commit to one method or 
another, and you'll deal with the learning curve and make a system that 
does what you need.

-- 
Allen Shaw
Polymer (http://polymerdb.org)



More information about the talk mailing list