Source for file SimpleTemplate.php
Documentation is available at SimpleTemplate.php
* $Id: SimpleTemplate.php,v 1.2 2004/06/02 14:33:38 hfuecks Exp $
* Shows how HTMLSax can be used for template parsing
require_once('XML/HTMLSax3.php');
function setVar ($name,$value) {
$this->vars[$name] = $value;
// Notice fourth argument
function open (& $parser,$name,$attrs,$empty) {
// Should check more carefully but this is just an example...
if ( isset ($this->vars[$attrs['name']]) ) {
$this->output.= $this->vars[$attrs['name']];
foreach ( $attrs as $key => $value ) {
$tag .= " $key=\"$value\"";
// Notice fourth argument
function close (& $parser,$name,$empty) {
$this->output.= " </$name>";
function data (& $parser,$data) {
function escape (& $parser,$data) {
$this->output .= " <!$data>";
function pi (& $parser,$target,$data) {
$this->output .= " <?$target $data?>";
function jasp (& $parser,$data) {
$this->output .= " <%$data%>";
$tpl=new SimpleTemplate ();
$tpl->setVar ('title','HTMLSax as a Template Parser');
HTMLSax can be used as the basis for a template engine,
as with <a href="http://wact.sf.net">WACT</a> and
<a href="http://phpoot.sourceforge.jp/">PHPOOT</a>. For
the most part is allows you to preserve the structure of
original template, preserving whitespace and so on with
one or two minor exceptions, such as whitespace between
attributes and the quotes used for attributes. Compare
the source template for this example with the output.
$tpl->setVar ('para1',$para1);
Notice also how the fourth argument to the open and close handlers
is used (see the PHP source) - this allows you to correctly
"rebuild" tags like <div /> vs. <div></div>
$tpl->setVar ('para2',$para2);
// Instantiate the parser
// Register the handler with the parser
$parser->set_object ($tpl);
$parser->set_option ('XML_OPTION_FULL_ESCAPES');
$parser->set_element_handler ('open','close');
$parser->set_data_handler ('data');
$parser->set_escape_handler ('escape');
$parser->set_pi_handler ('pi');
$parser->set_jasp_handler ('jasp');
Documentation generated on Mon, 11 Mar 2019 15:11:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|