#native_company# #native_desc#

Templates: The PHPLib Way Page 2

By David Orr
on July 30, 2000

Using PHPLIB Template

Let’s start with a cheesy example with some sample code. We’ll assume that there
is a template in the /home/mydir/mytemplates/ named MyTemplate.ihtml that has some
text that reads something like this:
Congratulations! You won a new {some_color} Honda Prelude!
Notice that “{some_color}” has curly braces around it. The curly braces indicate
that some_color is a template variable. We may want to write a PHP script that will
load the template, insert the value of the PHP variable $my_color where the {some_color}
template variable tag is, and then output the new text. If $my_color happens to be
set to “blue”, the final output should look like:
Congratulations! You won a new blue Honda Prelude!
Here’s the PHP script that will do just that:


include "template.inc"

$my_color "blue"// we'll use this later

$t = new Template("/home/mydir/mytemplates/");

// create a template object named $t


// set MyFileHandle = our template file


// set template variable some_color = $my_color value


// set template variable MyOutput = parsed file

$t->p("MyOutput"); // output the value of MyOutput (our parsed data)


The first line is an include command to give you PHPLIB Template functionality.
Of course PHPLIB does a lot more than templates, but if you only want to use the
template feature just include template.inc (template.inc is one of the files that
comes with PHPLIB). PHPLIB Template uses object-oriented programming, so the next
thing we do is create a Template object. The code <? $t = new Template("/home/mydir/mytemplates/"); ?> creates a new Template object $t. This $t object will be your handle for
accessing all of the template functions for the rest of the PHP script. If you
wanted to, you could create additional Template objects (each with their out
template variable namespace), but one is usually all you need. The path
(“/home/mydir/mytemplates/”) in the Template constructor call sets the root path
where your templates are located, but if you leave it out it defaults to the same
directory as your PHP script.
Then we call set_file() to define a handle “MyFileHandle” linked to MyTemplate.ihtml
(the template isn’t actually loaded until parse() is called). By the way, the .ihtml
extension of the template filename is customary for PHPLIB templates, but you can use
.html, .tpl, or any other extension. Then we call set_var() to set the template
variable some_color to the value of $my_color (which is “blue”), which means that all occurrences of {some_color} in the template will be replaced with the word blue when
we call parse().
And then we call parse(), which parses MyFileHandle by loading MyFileHandle
(MyTemplate.ihtml) and replacing any template variables ("{some_variable}") with
their template variable values, and the resulting parsed text is placed in MyOutput.
Nothing is output to the webserver until p("MyOutput") is called, which outputs the
final parsed text.