PHP Programming/Templates

Basic Templating
The simplest use of templates in PHP is very powerful for reducing errors and time spent on your pages. First, make sure your server has PHP enabled, etc., etc.

When you're ready to start, make one page that will be the template for all your pages. For example:

This is a title at the top of my page This is the body of my page This is a copyright notice

Now, say you want 2 more pages with the same header and footer. You don't have to code it again. You can save the header as one template and the footer as another. Just take all the header html up to the part where your body text begins:

&lt;p>This is a title at the top of my page&lt;/p>

Now save this as a separate file. I like to use the extension .inc (do the same with the footer)

&lt;p>This is the bottom of my page&lt;/p>

Now, in your main page, just type: And that's your page. Save it as a .php, upload it, and check it.

Managed Templating
Managed Templating allows you to create and use PHP Templates with a Template Engine. The PHP Developer/Designer doesn't have to create the engine for it to be used. The most reliable PHP Templating Engine is Smarty. Managed Template Systems are easy to use and are mostly used in big websites because of the need for dynamic paging. MediaWiki is one example of a Managed Template System. Managed Templating is easy to use for new and advanced users, for example:


 * index.php

The Value of Variable is : {$variable} The Value of Variable is : value
 * template.tpl
 * Output

Roll Your Own
Template engines work great, however if you are just looking for the basic Search and Replace template functionality, writing your own script is a snap.


 * Simple template function


 * Using the function


 * template.tpl, Template used for the above function


 * Parsed template output