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

  1. <?php
  2. /**
  3.  * Example of usage for HTML_Template_Sigma, block iteration
  4.  * 
  5.  * @package HTML_Template_Sigma
  6.  * @author Alexey Borzov <avb@php.net>
  7.  * 
  8.  * $Id: example_2.php,v 1.2 2003/05/19 15:06:39 avb Exp $
  9.  */ 
  10.  
  11. require_once 'HTML/Template/Sigma.php';
  12.  
  13. // various data to substitute
  14. $simpleAry  = array('foo', 'bar', 'baz', 'quux');
  15. $complexAry = array(
  16.     array('Error code', 'Error message', 'Reason', 'Solution'),
  17.     array('SIGMA_OK', ' ', 'Everything went OK', ' '),
  18.     array('SIGMA_BLOCK_NOT_FOUND', 'Cannot find block \'%s\'', 'Tried to access block that does not exist', 'Either add the block or fix the block name'),
  19.     array('SIGMA_BLOCK_DUPLICATE', 'The name of a block must be unique within a template. Block \'%s\' found twice.', 'Tried to load a template with several blocks sharing the same name', 'Get rid of one of the blocks or rename it')
  20. );
  21. $menuAry    = array(
  22.     'foo'  => 'First menu element',
  23.     'bar'  => 'Second menu element',
  24.     'baz'  => 'Another menu element',
  25.     'quux' => 'Yet another menu element'
  26. );
  27. $menuSelected = 'bar';
  28. $touchAry     = array(
  29.     array('apples', 10),
  30.     false,
  31.     array('oranges', 20)
  32. );
  33. $hideAry      = array(
  34.     array('restricted' => false, 'data' => array('item_id' => 'foo', 'item_title' => 'Some data')),
  35.     array('restricted' => true,  'data' => array('item_id' => 'bar', 'item_title' => 'More data')),
  36.     array('restricted' => true,  'data' => array('item_id' => 'baz', 'item_title' => 'Even more data')),
  37.     array('restricted' => false, 'data' => array('item_id' => 'quux', 'item_title' => 'Still even more data'))
  38. );
  39.  
  40. // instantiate the template object, templates will be loaded from the
  41. // 'templates' directory, no caching will take place
  42. $tpl =& new HTML_Template_Sigma('./templates');
  43.  
  44. // No errors are expected to happen here
  45. $tpl->setErrorHandling(PEAR_ERROR_DIE);
  46.  
  47. // default behaviour is to remove unknown variables and empty blocks 
  48. // from the template
  49. $tpl->loadTemplateFile('example_2.html');
  50.  
  51. // 1. Simple block iteration
  52. $tpl->setCurrentBlock('list');
  53. foreach ($simpleAry as $value) {
  54.     $tpl->setVariable('list_item', $value);
  55.     $tpl->parseCurrentBlock();
  56. }
  57.  
  58. // 2. Nested block iteration
  59. foreach ($complexAry as $inner) {
  60.     foreach ($inner as $value) {
  61.         $tpl->setVariable('table_item', $value);
  62.         // first we parse the innermost block
  63.         $tpl->parse('table_cell');
  64.     }
  65.     // then we parse the outer block
  66.     $tpl->parse('table_row');
  67. }
  68.  
  69. // 3. Menu-like structures
  70. foreach ($menuAry as $url => $title) {
  71.     // please note that only one inner block will be shown
  72.     // the other one will be considered empty and automatically removed
  73.     if ($menuSelected == $url) {
  74.         // we don't set menu_url to prevent menu_normal from appearing
  75.         // another possible approach here is to use hideBlock()
  76.         $tpl->setVariable('menu_title', $title);
  77.         $tpl->parse('menu_selected');
  78.     } else {
  79.         $tpl->setVariable(array(
  80.             'menu_url'   => $url,
  81.             'menu_title' => $title
  82.         ));
  83.         $tpl->parse('menu_normal');
  84.     }
  85.     // once again, the outer block is parsed after the inner
  86.     $tpl->parse('menu');
  87. }
  88.  
  89. // 4. Methods to manually control showing/removal of blocks
  90. // touchBlock() example
  91. foreach ($touchAry as $item) {
  92.     if (is_array($item)) {
  93.         $tpl->setVariable(array(
  94.             'touch_stuff'    => $item[0],
  95.             'touch_quantity' => $item[1]
  96.         ));
  97.     } else {
  98.         $tpl->touchBlock('empty_row');
  99.     }
  100.     $tpl->parse('touch_row');
  101. }
  102. // hideBlock() example
  103. foreach ($hideAry as $item) {
  104.     $tpl->setVariable($item['data']);
  105.     if ($item['restricted']) {
  106.         $tpl->hideBlock('edit_link');
  107.     }
  108.     $tpl->parse('hide_item');
  109. }
  110.  
  111. // 5. Using get() to move blocks around
  112. // This one is pretty simple. Note that by default get() does not clear the 
  113. // original block, so a copy will be created. If you want to move the block,
  114. // pass true as a second parameter to get()
  115. $tpl->parse('list_block');
  116. $tpl->setVariable('duplicate', $tpl->get('list_block'));
  117.  
  118. // output the results
  119. $tpl->show();
  120.  
  121. ?>
  122.