home support FAQ resources services partners contact us contact us
 Web Application Previous  Next  
 Tutorial

The Template Class

As shown in Chapter 6,“Database Access with PHP,” the PHPLib offers a solution for
many problems common to Web application. In our case, the Zend developers already
used the PHPLib for parts of their site, so we standardized on it for session management,
database abstractions, and HTML templates.

PHPLib’s Template class allows separation of code and layout, similar to the
EasyTemplate class we developed in Chapter 5, “Basic Web Application Strategies.”
This class has a richer feature set than our class; for example, it can contain blocks,
which mark sections to be replaced more than one time (useful for rows in tables, for
example), and it can open multiple files in one instance and combine them easily.The
drawback is that it’s less intuitive to use than EasyTemplate.
The Template class is completely separate from the rest of the PHPLib, and you can
use it without using any other PHPLib features. In case you’re interested in looking at
the source code for it, you can find it in the file template.inc in the PHPLib
distribution.

Just like EasyTemplate, PHPLib’s template class keeps HTML in separate files, using
placeholders for data that should be substituted dynamically by PHP. “Scalar” placeholders,
which will get replaced by ordinary strings, have the same format as the ones
in EasyTemplate.

The code in Listing 7.1 processes this simple template.
Listing 7.1 Basic example of the Template class.
// Create a template instance
$tpl = new Template();
// Load file, assign an identifier to it
$tpl->set_file(“page” => “basic_template.inc.html”);
// Assign contents to the placeholders
$tpl->set_var(array(“TITLE” => “This is a Template test”,
“CONTENTS” => “Hello World!”));
// Parse into a temporary variable (identifier)
$tpl->parse(“out”, “page”);
// Output the parsed template
$tpl->p(“out”);
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist