PHP Programming/Templates

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Basic Templating[edit | edit source]

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:

<html><body><p>This is a title at the top of my page</p>

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

<p>This is the bottom of my page</p></body></html>

Now, in your main page, just type:

 <?php require('header.inc'); ?>
 <p>this is the body of my page</p>
 <?php require('footer.inc'); ?>

And that's your page. Save it as a .php, upload it, and check it.

Notes[edit | edit source]

You can also use the include() or include_once() functions, if the page should continue loading, even if the file(s) can not be included.

The require(), include() and include_once() functions will work with other file types, and can be used anywhere on a page.

Advanced: Try using this with an if statement for DYNAMIC templating... ooh...

Managed Templating[edit | edit source]

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 ( [1] ). 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
 // This script is based on Smarty
 require_once("libs/Smarty.inc.php");
 // Compiled File Directory
 $smarty->compile_dir = "compiled";
 // Template Directory
 $smarty->template_dir = "templates";
 // Assign a Variable
 $smarty->assign("variable","value");
 // Display The Parsed Template
 $smarty->display("template.tpl");
  • template.tpl
The Value of Variable is : {$variable}
  • Output
The Value of Variable is : value

Roll Your Own[edit | edit source]

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
function Template($file, $array) {
	if (file_exists($file)) {
		$output = file_get_contents($file);
		foreach ($array as $key => $val) {
			$replace = '{'.$key.'}';
			$output = str_replace($replace, $val, $output);
		}
		return $output;
	}
}
  • Using the function
$fruit = 'Watermelon';
$color = 'Gray';

//parse and return template
$Template_Tpl = Template('template.tpl',
	array
	(
		'fruit'	=> $fruit,
		'color'	=> $color
	));

//display template
echo $Template_Tpl;
  • template.tpl, Template used for the above function
<p>
  <b>Your Favorite Food Is: {fruit}</b>
  <b>Your Favorite Color Is: {color}</b>
</p>
  • Parsed template output
<p>
  <b>Your Favorite Food Is: Watermelon</b>
  <b>Your Favorite Color Is: Gray</b>
</p>