Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Question About Embedding HTML In PHP

Tags:

html

php

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. :-)

like image 787
Brian Avatar asked Apr 17 '10 02:04

Brian


3 Answers

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.

like image 179
mpen Avatar answered Sep 24 '22 09:09

mpen


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.

like image 22
Thiago Silveira Avatar answered Sep 22 '22 09:09

Thiago Silveira


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.

like image 39
Ignacio Vazquez-Abrams Avatar answered Sep 24 '22 09:09

Ignacio Vazquez-Abrams