JV’s Power Tips for PHP (1) - Basic Template Theory
(Page 3 of 7 )
Back to the above print statement (print <<<END etc.). As a beginning PHP programmer this type of print statement can be used to build simple template driven websites very quickly. Here is a simple template using this concept:
<?php
// N.B. This file is stored as ‘templates/simple_template.php’
print <<<END
<html>
<head>
<title>Simple Template</title>
</head>
<body>
$gBODY
</body>
</html>
END;
?> Now let's output “Turkey Love” using our new simple_template.php:
<?php
$gBODY = “Turkey Love”;
include_once “templates/simple_template.php”;
?> This can be improved greatly upon by the use of two simple functions:
<?php
// N.B. This file is stored as ‘functions/global.php’
// =========================================
// Function 1 - Used to append something to a global variable
function out( $str , $var_name = “gBODY”)
{
// Make this global variable available to this function
global ${$var_name}; // <-- This is a variable variable
// Add the input of string to the global variable gBODY
// (or any other specified global variable)
${$var_name} .= $str;
}
// =========================================
// Function 2 - Used to select a template.
function use_template ($template_name)
{
if ( is_file(“templates/$template_name.php”))
{
include_once “templates/$template_name.php”;
}
else
{
echo “The template $template_name was not found!”;
}
}
?> Then if we place the above functions in a global function file, the whole thing looks much more elegant indeed.
<?php
include_once “functions/global.php”;
out(“Turkey Love”);
use_template(“simple_template”);
?> Using this method it is easy to see how we can create any number of templates and store them within the template directory. Let's have a look at this concept using a slightly more complicated template:
<?php
// N.B. This file is stored as ‘templates/complex_template.php’
print <<<END
<html>
<head>
<title>$gTITLE</title>
</head>
<body>
<table width=100%>
<tr>
<td width=100>
$gLEFT_MENU
</td>
<td>
$gBODY
</td>
</tr>
</table>
</body>
</html>
END;
?> Lets quickly define some global constants in functions/global.php (this will make sense in a minute).
<?php
// functions/global.php
define(“TITLE”,”gTITLE”);
define(“MENU”,”gLEFT_MENU”);
define(“BODY”,”gBODY”);
// etc..
?> Then to use complex_template.php, the script might look something like this:
<?php
include_once “functions/global.php”;
out(“Page Title”,TITLE);
out(“My Menu”,MENU);
out(“Hello World”,BODY);
use_template(“complex_template”);
?> If this doesn’t make sense to you go back to the function out(). You can see that any input to out() is written to a global variable (specified as a string in the second parameter of the function out()). Hopefully this makes it clear why I created constants using the define function. Essentially because I’m lazy and when I type:
out(“My Menu”,MENU); It is really the same as typing (with less effort [and more readable] than):
out(“My Menu”,”gLEFT_MENU”);Next: Refining the Previous >>
More PHP Articles
More By Justin Vincent