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

  1. <?php
  2. /**
  3.  * Example of usage for HTML_Template_Sigma, callbacks
  4.  * 
  5.  * @package HTML_Template_Sigma
  6.  * @author Alexey Borzov <avb@php.net>
  7.  * 
  8.  * $Id: example_4.php,v 1.1 2003/04/27 08:23:04 avb Exp $
  9.  */ 
  10.  
  11. require_once 'HTML/Template/Sigma.php';
  12.  
  13. function toggle($item1, $item2)
  14. {
  15.     static $i = 1;
  16.  
  17.     return $i++ % 2? $item1: $item2;
  18. }
  19.  
  20. // remember, this is an example only. there are lots of more advanced i18n solutions to use! :]
  21. function translate($str)
  22. {
  23.     global $lang, $aryI18n;
  24.  
  25.     return isset($aryI18n[$lang][$str])? $aryI18n[$lang][$str]: $str;
  26. }
  27.  
  28. $ary = array(
  29.     array('code' => 'SIGMA_OK', 'message' => ' ', 'reason' => 'Everything went OK', 'solution' => ' '),
  30.     array('code' => 'SIGMA_BLOCK_NOT_FOUND', 'message' => 'Cannot find block <i>\'blockname\'</i>', 'reason' => 'Tried to access block that does not exist', 'solution' => 'Either add the block or fix the block name'),
  31.     array('code' => 'SIGMA_BLOCK_DUPLICATE', 'message' => 'The name of a block must be unique within a template. Block <i>\'blockname\'</i> found twice.', 'reason' => 'Tried to load a template with several blocks sharing the same name', 'solution' => 'Get rid of one of the blocks or rename it'),
  32.     array('code' => 'SIGMA_INVALID_CALLBACK', 'message' => 'Callback does not exist', 'reason' => 'A callback function you wanted to use does not exist', 'solution' => 'Pass a name of an existing function to setCallbackfunction()')
  33. );
  34.  
  35. // I speak neither German, nor French. The strings are from phpBB translations (http://www.phpbb.com/)
  36. $aryI18n = array(
  37.     'de' => array(
  38.         'Send private message' => 'Private Nachricht senden',
  39.         'Username' => 'Benutzername',
  40.         'Find all posts by {username}' => 'Alle BeitrΣge von {username} anzeigen'
  41.     ),
  42.     'fr' => array(
  43.         'Send private message' => 'Envoyer un message privΘ',
  44.         'Username' => 'Nom d\'utilisateur',
  45.         'Find all posts by {username}' => 'Trouver tous les messages de {username}'
  46.     )
  47. );
  48. $langsAry = array('de' => 'German', 'fr' => 'French');
  49.  
  50. // instantiate the template object, templates will be loaded from the
  51. // 'templates' directory, no caching will take place
  52. $tpl =& new HTML_Template_Sigma('./templates');
  53.  
  54. // No errors are expected to happen here
  55. $tpl->setErrorHandling(PEAR_ERROR_DIE);
  56.  
  57. // default behaviour is to remove unknown variables and empty blocks 
  58. // from the template
  59. $tpl->loadTemplateFile('example_4.html');
  60.  
  61. // 1. Using callbacks for minor presentation changes
  62. $tpl->setCallbackFunction('bgcolor', 'toggle');
  63.  
  64. foreach ($ary as $item) {
  65.     $tpl->setVariable($item);
  66.     $tpl->parse('table_row');
  67. }
  68.  
  69. // 2. Using callbacks for i18n
  70. // We don't set a callback function, thus the function call will be replaced
  71. // by function's first argument (better than to throw an error, I think)
  72. $tpl->setVariable(array(
  73.     'language' => 'English (default)',
  74.     'username' => 'Luser'
  75. ));
  76. $tpl->parse('i18n_block');
  77.  
  78. // Now we set a callback function. Please note the third argument, we
  79. // want to process the strings with no variable substitutions done, they
  80. // should be done *after* the translation.
  81. $tpl->setCallbackFunction('translate', 'translate', true);
  82. foreach (array_keys($aryI18n) as $lang) {
  83.     $tpl->setVariable(array(
  84.         'language' => $langsAry[$lang],
  85.         'username' => 'Luser'
  86.     ));
  87.     $tpl->parse('i18n_block');
  88. }
  89.  
  90. // output the results
  91. $tpl->show();
  92.  
  93. ?>
  94.