tags:

views:

82

answers:

3

Hello all

Some time ago I read a posting on a board where the person was talking poorly about people that have HTML embedded/within their PHP. I do quite a bit of PHP development but I still interleave HTML and PHP in the same document. Is there a better way to do this, am I doing it wrong? I know that in JSP/JSF they use an XML document with namespaces to insert their HTML code so I was wondering if there was a similar function that PHP uses that I should be taking advantage of.

Thanks for taking the time to read. :-)

+1  A: 

You should consider using a template engine such as Smarty instead of mixing logic and presentation. This clears up both code and page, and forces you to define your requirements for the page clearly before invoking the template engine.

Ignacio Vazquez-Abrams
+3  A: 

Yes. You could separate the presentation code into different files. They call them views, or templates. There are a pretty bunch of templating engines you could use: there's smarty, there's Twig, and a lot of others.

You could also use a full-featured framework, like Zend, Symfony, CakePHP, Code Igniter etc. There's a lot of lists floating around.

Best regards,
T.

Thiago Silveira
I hope the +1 I gave you will help with the links :).
Mark Tomlin
Thanks for the help, I'll check them out. I'll also google "templates" and "PHP" to see how I can implement them myself.Thanks again.
Brian
Thanks Mark, I fixed the links now. No problem Brian, anything, just ask. :-)
Thiago Silveira
Symfony is an excellent MVC framework.
Christopher Altman
+5  A: 

PHP was originally designed as a templatng language. It sort of evolved over time to give it more power, but that doesn't mean you can't still use it for templating. A few <?=$var?>s here an there isn't too awful; hardly worse than {{var}} or whatever other syntax these new fangled engines offer.

The thing you really should do though, is take as much "business logic" out of the "views" as possible. That means the only code on the display page should be stuff that's actually relevant to how the page looks. No database updates or stuff like that. If you do this, then it should have nice, clean, maintainable pages :) No framework or anything necessary.

That said, I'd only do this for smaller apps. Template engines don't hurt either ;) Especially if your designer is a non-programmer.

Mark
PHP is still a templating language. Lightweight template engines like FryPHP are designed around that fact.
Frank Farmer
if it works for Wordpress, it works for me.
Samuel
Short tags aren't awful? I feel obligated to protest that statement.
Syntax Error
@Syntax Error: Oh I love em, but others don't seem to, so I try not to boast them *too* much.
Mark