Why separate presentation from logic?The simple answer to the question is, "It keeps things simple". If presentation wasn't separated from logic around your house, you'd have to be an electrician to replace light-switch covers. The same goes for large-scale web applications. Graphic designers shouldn't need to be software engineers in order to update the fonts in a web page. Separating logic from presentation makes that possible.
First, what is the difference between presentation and logic. Its the difference between dealing with the appearance of an apple and dealing with its genetic code. With web applications, presentation includes HTML tags, basic Javascript such as rollover effects, FLASH and anything else in that vein. Logic includes all of the software written in Python, Perl, PHP, or the p-p-p-particular language of choice which best solves the problem at hand. So when writing web applications, why separate the two?
The simple answer to the question of why presentation should be kept separate from logic is, "It keeps things simple". If presentation wasn't separated from logic around your house, you'd have to be an electrician to replace light-switch covers. The same goes for large-scale web applications. Graphic designers shouldn't need to be software engineers in order to update the fonts in a web page. Separating logic from presentation makes that possible.
There are many ways to accomplish this, but in this article we are going to focus on one method using PHP and an external class called FastTemplate. This class can be downloaded from: http://www.thewebmasters.net/php/
Reminder: Sometimes separating presentation from logic is overkill. Determine the scope of your project.
{mospagebreak title=Placing FastTemplate Variables in HTML Templates} It's very simple to include data from an application's business logic into HTML templates. HTML templates are files consisting of HTML with placeholders for dynamic data.
Whenever dynamic data needs a place holder in the HTML template, place the variable name between curly braces.
Ex:
<HTML>
<BODY>
{HOUSE}
<P>
This is a house.
</P>
</BODY>
</HTML>
The variables can be anything that the FastTemplate class can
parse: alphanumeric symbols and the underscore, or {(A-Z0-9_)+} for you reg ex lovers. Always keep these variables descriptive of the data that will fill it. Remember, graphic designers will ideally be editing these templates, so they need to know exactly what is going there. Creating charts that index which templates contain specific variables is useful if the need ever arises to change the names of any of them.
{mospagebreak title=Including the Fast Template Module in PHP Code} So how does the data get from code into the HTML templates? First, the FastTemplate class file must be included in the PHP code. Accomplish this the same as including any external class file. Here is the code to include the FastTemplate class file in a PHP script:
<?php
include "class.FastTemplate.php3";
Reminder: Be certain to place the path to the FastTemplate
class file into the PHP.ini file's INCLUDE_PATH variable.