nazly.me

Weblog of Nazly Ahmed

Nazly Ahmed

Nazly Ahmed

Web Developer. PHP Addict. Wordpress Hacker. FOSS Enthusiast. (Micro)Blogger. Photo Hobbyist. Cricket Fanatic. Husband. Dad.

I just wrote a very basic function to handle templates which can be used in PHP to seperate PHP code and HTML code. Embedding PHP into HTML or embedding HTML into PHP is a neccessity when it comes to developing Web Applications. The common problem faced by using either of this methods is that maintaing the code becomes very painful. Web Designers who design the Webpages prefer editing only HTML code rather than messing with PHP code in it. The solution for that will be using a Template Engine. Template Engines can be used to seperate the Application Logic and the Presentation Logic which makes the code more readable. I have been using Smarty as a Template Engine for a quite a long time in my Applications. Even in this Blog Application I used Smarty. I found it a bit difficult to use Smarty for quick solutions and small websites. I wanted to write a function that will do the templating part with a very basic setup. Its more a put this here, put this there kinda template.

Here is the function I wrote to get the job done. The real deal is that it supports caching as well. The place holders on the template file will be the label enclosed with [__ and __].

This is how the template file will look like

 
<html>
 
<body>
 
<table width="100%" align="center">
 
<tr>
 
<td><table width="100%" align="center">
 
<tr>
 
<td><h1>[__pageHeading__]</h1></td>
 
</tr>
 
<tr>
 
  <td>&nbsp;</td>
 
</tr>
 
<tr>
 
<td><strong>[__welcomeMsg__]</strong></td>
 
</tr>
 
<tr>
 
<td><table width="100%" align="center">
 
<tr>
 
<td width="200" align="left" valign="top" class="leftColumn">[__leftNav__]
 
</td>
 
<td valign="top"><table width="100%" align="center">
 
<tr>
 
<td>[__bodyContent__]</td>
 
</tr>
 
</table>
 
</td>
 
</tr>
 
</table>
 
</td>
 
</tr>
 
</table>
 
</td>
 
</tr>
 
</table>
 
</body>
 
</html>
Posted on 8th December 2005 12:23:52