home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / example_3.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  2.2 KB  |  70 lines

  1. <?php
  2. /**
  3.  * Example of usage for HTML_Template_Sigma, building the template from multiple files
  4.  * 
  5.  * @package HTML_Template_Sigma
  6.  * @author Alexey Borzov <avb@php.net>
  7.  * 
  8.  * $Id: example_3.php,v 1.1 2003/04/20 10:06:31 avb Exp $
  9.  */ 
  10.  
  11. require_once 'HTML/Template/Sigma.php';
  12.  
  13. // various data to substitute
  14. $addBlockAry = array(
  15.     '<!-- INCLUDE -->' => 'Includes a file from within a template',
  16.     'addBlockfile()'         => 'Creates a new block in place of a variable placeholder',
  17.     'replaceBlockfile()'     => 'Replaces the existing block with a new content'
  18. );
  19. $replaceBlockAry = array(
  20.     'foo', 'bar', 'baz', 'quux'
  21. );
  22.  
  23. // instantiate the template object, templates will be loaded from the
  24. // 'templates' directory, no caching will take place
  25. $tpl =& new HTML_Template_Sigma('./templates');
  26.  
  27. // No errors are expected to happen here
  28. $tpl->setErrorHandling(PEAR_ERROR_DIE);
  29.  
  30. // default behaviour is to remove unknown variables and empty blocks 
  31. // from the template
  32. $tpl->loadTemplateFile('example_3.html');
  33.  
  34. // 2. Using addBlockfile()
  35. // addblockfile placeholder will be gone, new_block block will appear in its place
  36. $tpl->addBlockfile('addblockfile', 'new_block', 'example_3_add.html');
  37. foreach ($addBlockAry as $name => $desc) {
  38.     $tpl->setVariable(array(
  39.         'func_name'        => $name,
  40.         'func_description' => $desc
  41.     ));
  42.     $tpl->parse('added_block');
  43. }
  44.  
  45. // 3. Using replaceBlockfile()
  46. // 3.1 Keeping the previously parsed contents
  47. for ($i = 0; $i < count($replaceBlockAry); $i++) {
  48.     if (2 == $i) {
  49.         // note the third argument, this is done to prevent clearing the parsed contents
  50.         $tpl->replaceBlockfile('replace_block_1', 'example_3_replace_1.html', true);
  51.     }
  52.     $tpl->setVariable('item_title', $replaceBlockAry[$i]);
  53.     $tpl->parse('replace_block_1');
  54. } // for
  55.  
  56. // 3.2 Discarding the previously parsed contents
  57. $tpl->setVariable('item_title', 'This will be discarded');
  58. $tpl->parse('replace_block_2');
  59. // default behaviour is to discard parsed contents
  60. $tpl->replaceBlockfile('replace_block_2', 'example_3_replace_2.html');
  61. foreach ($replaceBlockAry as $item) {
  62.     $tpl->setVariable('item_title', $item);
  63.     $tpl->parse('replace_block_2_item');
  64. }
  65.  
  66. // output the results
  67. $tpl->show();
  68.  
  69. ?>
  70.