home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 June / PCpro_2005_06.ISO / files / opensource / xamp / xampp-win32.exe / xampp / example_4.php < prev    next >
Encoding:
PHP Script  |  2004-10-01  |  3.7 KB  |  105 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.2 2004/04/14 09:41:55 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. function letters($str)
  29. {
  30.     return preg_replace('/[^\\w\\s]/', '', $str);
  31. }
  32.  
  33. $ary = array(
  34.     array('code' => 'SIGMA_OK', 'message' => ' ', 'reason' => 'Everything went OK', 'solution' => ' '),
  35.     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'),
  36.     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'),
  37.     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()')
  38. );
  39.  
  40. // I speak neither German, nor French. The strings are from phpBB translations (http://www.phpbb.com/)
  41. $aryI18n = array(
  42.     'de' => array(
  43.         'Send private message' => 'Private Nachricht senden',
  44.         'Username' => 'Benutzername',
  45.         'Find all posts by {username}' => 'Alle BeitrΣge von {username} anzeigen'
  46.     ),
  47.     'fr' => array(
  48.         'Send private message' => 'Envoyer un message privΘ',
  49.         'Username' => 'Nom d\'utilisateur',
  50.         'Find all posts by {username}' => 'Trouver tous les messages de {username}'
  51.     )
  52. );
  53. $langsAry = array('de' => 'German', 'fr' => 'French');
  54.  
  55. // instantiate the template object, templates will be loaded from the
  56. // 'templates' directory, no caching will take place
  57. $tpl =& new HTML_Template_Sigma('./templates');
  58.  
  59. // No errors are expected to happen here
  60. $tpl->setErrorHandling(PEAR_ERROR_DIE);
  61.  
  62. // default behaviour is to remove unknown variables and empty blocks 
  63. // from the template
  64. $tpl->loadTemplateFile('example_4.html');
  65.  
  66. // 1. Using callbacks for minor presentation changes
  67. $tpl->setCallbackFunction('bgcolor', 'toggle');
  68.  
  69. foreach ($ary as $item) {
  70.     $tpl->setVariable($item);
  71.     $tpl->parse('table_row');
  72. }
  73.  
  74. // 2. Using callbacks for i18n
  75. // We don't set a callback function, thus the function call will be replaced
  76. // by function's first argument (better than to throw an error, I think)
  77. $tpl->setVariable(array(
  78.     'language' => 'English (default)',
  79.     'username' => 'Luser'
  80. ));
  81. $tpl->parse('i18n_block');
  82.  
  83. // Now we set a callback function. Please note the third argument, we
  84. // want to process the strings with no variable substitutions done, they
  85. // should be done *after* the translation.
  86. $tpl->setCallbackFunction('translate', 'translate', true);
  87. foreach (array_keys($aryI18n) as $lang) {
  88.     $tpl->setVariable(array(
  89.         'language' => $langsAry[$lang],
  90.         'username' => 'Luser'
  91.     ));
  92.     $tpl->parse('i18n_block');
  93. }
  94.  
  95. // 3. Shorthand for callbacks, built-in callbacks
  96. // We add a variable that cannot be safely displayed either in HTML,
  97. // in URLs or inside JavaScript string constants without appropriate encoding
  98. $tpl->setVariable('escaped', '"Foo & Bar"');
  99. $tpl->setCallbackFunction('letters', 'letters');
  100.  
  101. // output the results
  102. $tpl->show();
  103.  
  104. ?>
  105.