Re: better way to mix html and php code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 05:04 PM 11/16/2005, Brent Baisley wrote:
You should separate HTML and PHP code into separate files to make it
easily maintainable. Ideally, someone who knows HTML without any
knowledge of PHP would be able to change the layout of the web page
without breaking anything.
There are a bunch of examples of how to do this, usually falling
under the "MVC Design Pattern" title. That's Model, View, Controller.

I use a simple substitution system to embed "tags" that represent
data into my HTML files.

The html file would look something like:
<table>
<tr><td>First Name</td>
<td>{:FirstName:}</td>
<tr><td>Last Name</td>
<td>{:LastName:}</td>
</table>

The php file would be something like:
$tpl    = file_get_contents('htmlfile.htm');
//Assign Data to tags
$data['{:FirstName:}']    = 'Brent';
$data['{:LastName:}']    = 'Baisley';
//Get Tags to search on
$tags    = array_keys($data);

//Populate html template with data
$content    = str_replace($tags, $data, $tpl);
echo $tpl;


That's an extremely simplified templating system and an over
simplified example. But it shows how you can easily completely
separate html from php, presentation from logic. I find it far easier
to work on than any of your three examples. Most importantly, it
allows more talented interface designer design your interface, while
you focus on the php and logic.

<a lot of stuff snipped>

This

<td>{:FirstName:}</td>
is really so different from

<td> <?php echo $FirstName ?> </td>

Why learn a templating language on top of PHP? That's one of its reasons for being.

Yes, it can be a bit of a hash at times. Of all the suggestion offered, if I was dumped in and had to do so maintenance, Curt's was the cleanest example.

Cheers - Miles

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux