Re: better way to mix html and php code?
| From: | Miles Thompson | Date: | Wed, 16 Nov 2005 21:17:57 +0000 |
| Subject: | Re: better way to mix html and php code? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-225936@lists.php.net to get a copy of this message | ||
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:This$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>
<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