XML_HTMLSax3
[ class tree: XML_HTMLSax3 ] [ index: XML_HTMLSax3 ] [ all elements ]

Source for file SimpleTemplate.php

Documentation is available at SimpleTemplate.php

  1. <?php
  2. /***
  3.  * $Id: SimpleTemplate.php,v 1.2 2004/06/02 14:33:38 hfuecks Exp $
  4.  * Shows how HTMLSax can be used for template parsing
  5.  */
  6. require_once('XML/HTMLSax3.php');
  7.  
  8. class SimpleTemplate {
  9.     var $vars = array();
  10.  
  11.     var $output '';
  12.  
  13.     function setVar($name,$value{
  14.         $this->vars[$name$value;
  15.     }
  16.  
  17.     function display({
  18.         echo $this->output;
  19.     }
  20.  
  21.     // Notice fourth argument
  22.     function open($parser,$name,$attrs,$empty{
  23.  
  24.         // Should check more carefully but this is just an example...
  25.         if $name == 'var' {
  26.             if isset($this->vars[$attrs['name']]) ) {
  27.                 $this->output.= $this->vars[$attrs['name']];
  28.             }
  29.         else {
  30.             $tag = "<$name";
  31.             foreach $attrs as $key => $value {
  32.                 if is_null($value) ) {
  33.                     $tag .= ' '.$key;
  34.                 else {
  35.                     $tag .= " $key=\"$value\"";
  36.                 }
  37.             }
  38.             if $empty {
  39.                 $tag .= '/>';
  40.             else {
  41.                 $tag .= '>';
  42.             }
  43.             $this->output .= $tag;
  44.         }
  45.     }
  46.  
  47.     // Notice fourth argument
  48.     function close($parser,$name,$empty{
  49.         if !$empty {
  50.             $this->output.= "</$name>";
  51.         }
  52.     }
  53.  
  54.     function data($parser,$data{
  55.         $this->output .= $data;
  56.     }
  57.  
  58.     function escape($parser,$data{
  59.         $this->output .= "<!$data>";
  60.     }
  61.  
  62.     function pi($parser,$target,$data{
  63.         $this->output .= "<?$target $data?>";
  64.     }
  65.  
  66.     function jasp($parser,$data{
  67.         $this->output .= "<%$data%>";
  68.     }
  69. }
  70.  
  71.  
  72. $tpl=new SimpleTemplate();
  73.  
  74. $tpl->setVar('title','HTMLSax as a Template Parser');
  75.  
  76. $para1 = <<<EOD
  77.  
  78. HTMLSax can be used as the basis for a template engine, 
  79. as with <a href="http://wact.sf.net">WACT</a> and 
  80. <a href="http://phpoot.sourceforge.jp/">PHPOOT</a>. For 
  81. the most part is allows you to preserve the structure of
  82. original template, preserving whitespace and so on with
  83. one or two minor exceptions, such as whitespace between
  84. attributes and the quotes used for attributes. Compare
  85. the source template for this example with the output.
  86.  
  87. EOD;
  88. $tpl->setVar('para1',$para1);
  89.  
  90. $para2 = <<<EOD
  91.  
  92. Notice also how the fourth argument to the open and close handlers
  93. is used (see the PHP source) - this allows you to correctly
  94. "rebuild" tags like &lt;div /&gt; vs. &lt;div&gt;&lt;/div&gt;
  95.  
  96. EOD;
  97. $tpl->setVar('para2',$para2);
  98.  
  99. // Instantiate the parser
  100. $parser=new XML_HTMLSax3();
  101.  
  102. // Register the handler with the parser
  103. $parser->set_object($tpl);
  104.  
  105. // Set a parser option
  106. $parser->set_option('XML_OPTION_FULL_ESCAPES');
  107.  
  108. // Set the handlers
  109. $parser->set_element_handler('open','close');
  110. $parser->set_data_handler('data');
  111. $parser->set_escape_handler('escape');
  112. $parser->set_pi_handler('pi');
  113. $parser->set_jasp_handler('jasp');
  114.  
  115. // Parse the document
  116. $parser->parse(file_get_contents('simpletemplate.tpl'));
  117.  
  118. $tpl->display();
  119. ?>

Documentation generated on Mon, 11 Mar 2019 15:11:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.