Re: Using PHP/HTML effectivly
| From: | Jon Anderson | Date: | Wed, 14 Jun 2006 18:48:09 +0000 |
| Subject: | Re: Using PHP/HTML effectivly | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-238002@lists.php.net to get a copy of this message | ||
This is my opinion on the matter...
From experience, I would say that mixing PHP and HTML together in a complicated page can often get very ugly. I prefer to separate out presentation and code into separate layers as much as possible.
I have most often used template systems such as Smarty (smarty.php.net) for that. I use Smarty for work - I find it quite nice. It allows you to separate your code from your presentation very effectively. It's easy to use, and provides things like caching for performance.
However, I came to the realization recently that PHP itself is essentially a very feature rich template system (nevermind the fact that it's a well defined language. ;-)
By splitting up my most recent project into includes, templates, and pages, I get a solution to the problem in pure PHP. In the end, code looks like code, and HTML looks (almost) like HTML and both are readable. (Same result as what Smarty gives you, without needing Smarty.)
Most pages look something like:
<?php
include('include/data_functions.php');
if ($condition) {
$title = 'Here is Some Data!';
$data = getSomeData($var1,$var2);
} else {
$title = 'Here is the Other Data!';
$data = getOtherData($var1,$var2);
}
include('template/page.php');
?>
And most templates look something like:
<?php include('template/head.php'); ?>
<h1><?= $title ?></h1>
...
<table>
<?php foreach ($data as $item) { ?>
<tr>
<td><?= $item['part1'] ?></td>
<td><?= $item['part2'] ?></td>
</tr>
<?php } /* END foreach ($data as $item) */ ?>
</table>
...
<p>Thanks for your interest in my data!</p>
<?php include('template/foot.php'); ?>
jon
Alex Major wrote:
Hi List,I've been (very slowly) working my way through some basic php, andputting it into my html site. However recently (after trying things out such as cookies or redirects where they have to be set before any page output) I've found that the combination or certainly the way that I'm using php and html together dosn't seem to go too well. I have a question to you experienced PHP developers, how do you mix the html/php together. When I've looked at things like the PHPBB forums, and gone through files they will have absolutly no HTML, just php code. I'm not sure how they manage their page styling, if anyone could explain this too me. At the moment I have things such as <table ...> <?php Some php code making a page header ?> </table> <table ...> <?php Content... ?> </table> I'm sure that I'm not doing it the best possible way, so if anyone has any tips for me.