I figured some could benefit from this article I recently wrote... hope this helps =). Have a good one.
For a PRETTIER version go to http://www.mustangmike.org/tutorials..._with_php.html
Purpose of article: Demonstrate how to build template-driven scripts with PHP.
Why build template-driven scripts: Most people realize that HTML can
be used in PHP scripts and actually do embed HTML in their PHP scripts.
Sometimes though it is far more useful to be able to simply edit HTML files
instead of sifting through PHP code. By using templates you can very easily
change the look of your pages without even looking at any PHP code. This
is especially useful when you work in a team where somebody handles the
programming aspect and another person handles the design aspect.
Template concepts: It is very simple actually; you make your PHP script
read the template file and print it out to the browser. "But what about dynamic
content?" you may be wondering. The answer is simple: tokens. Tokens are
essentially just placeholders on a template and are usually signified by a word
surrounded by two percent (%) signs. For example, you may put a %date%
token on your template and when it goes through the PHP script it gets changed
to the actual date. You could do the same thing for member IDs, names, links,
passing data on forms, etc, etc... the list is endless.
Lets code it: Not only do I explain the uses and concepts of templates, but I even
provide working code to make it happen in a ready-to-use format. Below is the
PHP function I frequently use to display templates:
#################################################
function ShowPage ($file, $tokens=array("%no_tokens_passed%" => "%no_tokens_passed%")) {
// See if the file exists first. If not, return 0.
if( !is_readable($file )) {
return 0;
}
// See if any tokens were passed.
$tokens_exist = count($tokens); // counts the keys
// Open the file for reading
$fh = fopen($file, "r") or die("Failed opening template");
// Read the file line by line. Check to see if any tokens were passed before trying to parse any lines.
while( !feof($fh) ) {
$line = fgets($fh, 1024);
foreach ($tokens as $key=>$val) {
$line = preg_replace("/".$key."/i",$val,$line); // replace all tokens on each line
}
print $line;
}
// Close the file
fclose($fh);
}
The first argument passed to the function is the actual template filename. Passing
the full path is the best choice, but isn't necessary. The second argument is optional,
but is an associative array (hash) of tokens and values.
An example for using this function would be as follows:
$template_dir = '/home/virtual/site61/fst/var/www/html/templates';
ShowPage("$template_dir/member_signup_confirmation.html",
array("%first_name%" => $_REQUEST['first_name'],
"%last_name%" => $_REQUEST['last_name'],
"%email%" => $_REQUEST['email']));
As you can see, the first argument is the full path to the HTML template called
member_signup_confirmation.html. This could be a confirmation page where
the user is sent after filling out a signup for to confirm his/her information before
submitting. The second argument is a single ARRAY (hash) which contains a
list of keys & values for the tokens. Inside the member_signup_confirmation.html
template would be the tokens %first_name%, %last_name%, and %email%.
Conclusion: You are now fully equipped with the knowledge and resources it
takes to start building template-driven scripts with PHP so get out there & start
building =). If you are needing a quick solution and you don't want to code it then
you can also check out http://www.PHPLabs.com/ for some of the best PHP scripts
available on the Internet. And while on a completely other topic, you should also
check out http://www.Cellulean.com/ to see an actual WORKING anti-fat cream..
even several news stations nationwide have taken notice!
Copyright Mike Rogne 2005. All Rights Reserved.
For a PRETTIER version go to http://www.mustangmike.org/tutorials..._with_php.html
Purpose of article: Demonstrate how to build template-driven scripts with PHP.
Why build template-driven scripts: Most people realize that HTML can
be used in PHP scripts and actually do embed HTML in their PHP scripts.
Sometimes though it is far more useful to be able to simply edit HTML files
instead of sifting through PHP code. By using templates you can very easily
change the look of your pages without even looking at any PHP code. This
is especially useful when you work in a team where somebody handles the
programming aspect and another person handles the design aspect.
Template concepts: It is very simple actually; you make your PHP script
read the template file and print it out to the browser. "But what about dynamic
content?" you may be wondering. The answer is simple: tokens. Tokens are
essentially just placeholders on a template and are usually signified by a word
surrounded by two percent (%) signs. For example, you may put a %date%
token on your template and when it goes through the PHP script it gets changed
to the actual date. You could do the same thing for member IDs, names, links,
passing data on forms, etc, etc... the list is endless.
Lets code it: Not only do I explain the uses and concepts of templates, but I even
provide working code to make it happen in a ready-to-use format. Below is the
PHP function I frequently use to display templates:
#################################################
function ShowPage ($file, $tokens=array("%no_tokens_passed%" => "%no_tokens_passed%")) {
// See if the file exists first. If not, return 0.
if( !is_readable($file )) {
return 0;
}
// See if any tokens were passed.
$tokens_exist = count($tokens); // counts the keys
// Open the file for reading
$fh = fopen($file, "r") or die("Failed opening template");
// Read the file line by line. Check to see if any tokens were passed before trying to parse any lines.
while( !feof($fh) ) {
$line = fgets($fh, 1024);
foreach ($tokens as $key=>$val) {
$line = preg_replace("/".$key."/i",$val,$line); // replace all tokens on each line
}
print $line;
}
// Close the file
fclose($fh);
}
The first argument passed to the function is the actual template filename. Passing
the full path is the best choice, but isn't necessary. The second argument is optional,
but is an associative array (hash) of tokens and values.
An example for using this function would be as follows:
$template_dir = '/home/virtual/site61/fst/var/www/html/templates';
ShowPage("$template_dir/member_signup_confirmation.html",
array("%first_name%" => $_REQUEST['first_name'],
"%last_name%" => $_REQUEST['last_name'],
"%email%" => $_REQUEST['email']));
As you can see, the first argument is the full path to the HTML template called
member_signup_confirmation.html. This could be a confirmation page where
the user is sent after filling out a signup for to confirm his/her information before
submitting. The second argument is a single ARRAY (hash) which contains a
list of keys & values for the tokens. Inside the member_signup_confirmation.html
template would be the tokens %first_name%, %last_name%, and %email%.
Conclusion: You are now fully equipped with the knowledge and resources it
takes to start building template-driven scripts with PHP so get out there & start
building =). If you are needing a quick solution and you don't want to code it then
you can also check out http://www.PHPLabs.com/ for some of the best PHP scripts
available on the Internet. And while on a completely other topic, you should also
check out http://www.Cellulean.com/ to see an actual WORKING anti-fat cream..
even several news stations nationwide have taken notice!
Copyright Mike Rogne 2005. All Rights Reserved.

Comment