PHP_Template.class.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * Copyright 2003, Paul James
  4. *
  5. * This file contains some methods from the Smarty templating engine version
  6. * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>.
  7. *
  8. * The SquirrelMail (Foowd) template implementation.
  9. * Derived from the foowd template implementation and adapted
  10. * for squirrelmail
  11. * @copyright &copy; 2005-2007 The SquirrelMail Project Team
  12. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  13. * @version $Id$
  14. * @package squirrelmail
  15. *
  16. */
  17. /**
  18. * The SquirrelMail PHP Template class. Extends the base
  19. * Template class for use with PHP template pages.
  20. *
  21. * @author Paul James
  22. * @author Monte Ohrt <monte at ispi.net>
  23. * @author Andrei Zmievski <andrei at php.net>
  24. * @author Paul Lesniewski <paul at squirrelmail.org>
  25. * @package squirrelmail
  26. *
  27. */
  28. class PHP_Template extends Template
  29. {
  30. /**
  31. * The templates values array
  32. *
  33. * @var array
  34. *
  35. */
  36. var $values = array();
  37. /**
  38. * Constructor
  39. *
  40. * Please do not call directly. Use Template::construct_template().
  41. *
  42. * @param string $template_id the template ID
  43. *
  44. */
  45. function PHP_Template($template_id) {
  46. //FIXME: find a way to test that this is ONLY ever called
  47. // from parent's construct_template() method (I doubt it
  48. // is worth the trouble to parse the current stack trace)
  49. // if (???)
  50. // trigger_error('Please do not use default PHP_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
  51. parent::Template($template_id);
  52. }
  53. /**
  54. * Assigns values to template variables
  55. *
  56. * @param array|string $tpl_var the template variable name(s)
  57. * @param mixed $value the value to assign
  58. FIXME: Proposed idea to add a parameter here that turns variable
  59. encoding on, so that we can make sure output is always
  60. run through something like htmlspecialchars() (maybe even nl2br()?)
  61. *
  62. */
  63. function assign($tpl_var, $value = NULL) {
  64. if (is_array($tpl_var))
  65. {
  66. foreach ($tpl_var as $key => $val)
  67. {
  68. if ($key != '')
  69. $this->values[$key] = $val;
  70. }
  71. }
  72. else
  73. {
  74. if ($tpl_var != '')
  75. $this->values[$tpl_var] = $value;
  76. }
  77. }
  78. /**
  79. * Assigns values to template variables by reference
  80. *
  81. * @param string $tpl_var the template variable name
  82. * @param mixed $value the referenced value to assign
  83. FIXME: Proposed idea to add a parameter here that turns variable
  84. encoding on, so that we can make sure output is always
  85. run through something like htmlspecialchars() (maybe even nl2br()?)
  86. *
  87. */
  88. function assign_by_ref($tpl_var, &$value) {
  89. if ($tpl_var != '')
  90. $this->values[$tpl_var] = &$value;
  91. }
  92. /**
  93. * Clears the values of all assigned varaiables.
  94. *
  95. */
  96. function clear_all_assign() {
  97. $this->values = array();
  98. }
  99. /**
  100. * Returns assigned variable value(s).
  101. *
  102. * @param string $varname If given, the value of that variable
  103. * is returned, assuming it has been
  104. * previously assigned. If not specified
  105. * an array of all assigned variables is
  106. * returned. (optional)
  107. *
  108. * @return mixed Desired single variable value or list of all
  109. * assigned variable values.
  110. *
  111. */
  112. function get_template_vars($varname=NULL) {
  113. // just looking for one value
  114. //
  115. if (!empty($varname)) {
  116. if (!empty($this->values[$varname]))
  117. return $this->values[$varname];
  118. else
  119. // FIXME: this OK? What does Smarty do?
  120. return NULL;
  121. }
  122. // return all variable values
  123. //
  124. return $this->values;
  125. }
  126. /**
  127. * Appends values to template variables
  128. *
  129. * @param array|string $tpl_var the template variable name(s)
  130. * @param mixed $value the value to append
  131. * @param boolean $merge when $value is given as an array,
  132. * this indicates whether or not that
  133. * array itself should be appended as
  134. * a new template variable value or if
  135. * that array's values should be merged
  136. * into the existing array of template
  137. * variable values
  138. FIXME: Proposed idea to add a parameter here that turns variable
  139. encoding on, so that we can make sure output is always
  140. run through something like htmlspecialchars() (maybe even nl2br()?)
  141. *
  142. */
  143. function append($tpl_var, $value = NULL, $merge = FALSE)
  144. {
  145. if (is_array($tpl_var))
  146. {
  147. foreach ($tpl_var as $_key => $_val)
  148. {
  149. if ($_key != '')
  150. {
  151. if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
  152. settype($this->values[$_key],'array');
  153. if($merge && is_array($_val))
  154. {
  155. // FIXME: Tentative testing seems to indicate that
  156. // this does not mirror Smarty behavior; Smarty
  157. // seems to append the full array as a new element
  158. // instead of merging, so this behavior is technically
  159. // more "correct", but Smarty seems to differ
  160. foreach($_val as $_mkey => $_mval)
  161. $this->values[$_key][$_mkey] = $_mval;
  162. }
  163. else
  164. $this->values[$_key][] = $_val;
  165. }
  166. }
  167. }
  168. else
  169. {
  170. if ($tpl_var != '' && isset($value))
  171. {
  172. if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
  173. settype($this->values[$tpl_var],'array');
  174. if($merge && is_array($value))
  175. {
  176. foreach($value as $_mkey => $_mval)
  177. $this->values[$tpl_var][$_mkey] = $_mval;
  178. }
  179. else
  180. $this->values[$tpl_var][] = $value;
  181. }
  182. }
  183. }
  184. /**
  185. * Appends values to template variables by reference
  186. *
  187. * @param string $tpl_var the template variable name
  188. * @param mixed $value the referenced value to append
  189. * @param boolean $merge when $value is given as an array,
  190. * this indicates whether or not that
  191. * array itself should be appended as
  192. * a new template variable value or if
  193. * that array's values should be merged
  194. * into the existing array of template
  195. * variable values
  196. FIXME: Proposed idea to add a parameter here that turns variable
  197. encoding on, so that we can make sure output is always
  198. run through something like htmlspecialchars() (maybe even nl2br()?)
  199. *
  200. */
  201. function append_by_ref($tpl_var, &$value, $merge = FALSE)
  202. {
  203. if ($tpl_var != '' && isset($value))
  204. {
  205. if(!@is_array($this->values[$tpl_var]))
  206. settype($this->values[$tpl_var],'array');
  207. if ($merge && is_array($value))
  208. {
  209. foreach($value as $_key => $_val)
  210. $this->values[$tpl_var][$_key] = &$value[$_key];
  211. }
  212. else
  213. $this->values[$tpl_var][] = &$value;
  214. }
  215. }
  216. /**
  217. * Applys the template and generates final output destined
  218. * for the user's browser
  219. *
  220. * @param string $filepath The full file path to the template to be applied
  221. *
  222. * @return string The output for the given template
  223. *
  224. */
  225. function apply_template($filepath) {
  226. // place values array directly in scope
  227. // ($t? let's try to be more verbose please :-) )
  228. //
  229. $t = &$this->values;
  230. ob_start();
  231. include($filepath);
  232. $contents = ob_get_contents();
  233. ob_end_clean();
  234. return $contents;
  235. }
  236. }