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

  1. <?php
  2. /**
  3.  * Example of usage for HTML_Template_Sigma, basic variables and blocks
  4.  * 
  5.  * @package HTML_Template_Sigma
  6.  * @author Alexey Borzov <avb@php.net>
  7.  * 
  8.  * $Id: example_1.php,v 1.1 2003/04/20 12:40:32 avb Exp $
  9.  */ 
  10.  
  11. require_once 'HTML/Template/Sigma.php';
  12.  
  13. $listAry = array(
  14.     array('foo', 'bar'),
  15.     'stuff',
  16.     array('baz', 'quux'),
  17.     'more stuff'
  18. );
  19.  
  20. // instantiate the template object, templates will be loaded from the
  21. // 'templates' directory, no caching will take place
  22. $tpl =& new HTML_Template_Sigma('./templates');
  23.  
  24. // No errors are expected to happen here
  25. $tpl->setErrorHandling(PEAR_ERROR_DIE);
  26.  
  27. // default behaviour is to remove unknown variables and empty blocks 
  28. // from the template
  29. $tpl->loadTemplateFile('example_1.html');
  30.  
  31. // 1. Variable substitution
  32. // you can pass a name and a value to setVariable()
  33. $tpl->setVariable('var1', 'Value 1');
  34. // you can also pass an associative array
  35. $tpl->setVariable(array(
  36.     'var2' => 'Value 2',
  37.     'var3' => 'Value 3'
  38. ));
  39. // setGlobalVariable works the same
  40. $tpl->setGlobalVariable('glob', 'I am global');
  41.  
  42. // 2. Empty/nonempty blocks
  43. // 2.1 Non-empty blocks
  44. $tpl->setVariable(array(
  45.     'var_ne_1' => 'Value for block 1',
  46.     'var_ne_2' => 'Value for subblock 2'
  47. ));
  48. // 2.2 Empty blocks
  49. $tpl->setVariable('var_e_2', 'Value for parent block');
  50.  
  51. // 3. Local/global difference
  52. $tpl->setCurrentBlock('list');
  53. foreach ($listAry as $item) {
  54.     if (is_array($item)) {
  55.         $tpl->setVariable(array(
  56.             'local_1' => $item[0],
  57.             'local_2' => $item[1]
  58.         ));
  59.     } else {
  60.         $tpl->setVariable('local_1', $item);
  61.     }
  62.     $tpl->parseCurrentBlock();
  63. }
  64.  
  65. // output the results
  66. $tpl->show();
  67.  
  68. ?>
  69.