Smarty_Template.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * Smarty_Template.class.php
  4. *
  5. * This file contains a Template subclass intended as a bridge between
  6. * SquirrelMail and Smarty. All abstract methods from the Template class
  7. * are implemented here.
  8. *
  9. * @copyright &copy; 2003-2007 The SquirrelMail Project Team
  10. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  11. * @version $Id$
  12. * @package squirrelmail
  13. * @subpackage Template
  14. * @since 1.5.2
  15. *
  16. */
  17. /**
  18. * The SquirrelMail Smarty Template class. Extends the base
  19. * Template class for use with Smarty template pages.
  20. *
  21. * @author Paul Lesniewski <paul at squirrelmail.org>
  22. * @package squirrelmail
  23. *
  24. */
  25. class Smarty_Template extends Template
  26. {
  27. /**
  28. * The Smarty template object
  29. *
  30. * @var object
  31. *
  32. */
  33. var $smarty_template = null;
  34. /**
  35. * Constructor
  36. *
  37. * Please do not call directly. Use Template::construct_template().
  38. *
  39. * @param string $template_id the template ID
  40. *
  41. */
  42. function Smarty_Template($template_id) {
  43. //FIXME: find a way to test that this is ONLY ever called
  44. // from parent's construct_template() method (I doubt it
  45. // is worth the trouble to parse the current stack trace)
  46. // if (???)
  47. // trigger_error('Please do not use default Smarty_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
  48. parent::Template($template_id);
  49. // load smarty settings
  50. //
  51. // instantiate and set up Smarty object
  52. //
  53. $smarty_path
  54. = Template::get_template_config($this->template_set_id, 'smarty_path');
  55. require($smarty_path);
  56. $this->smarty_template = new Smarty();
  57. $this->smarty_template->compile_dir
  58. = Template::get_template_config($this->template_set_id, 'smarty_compile_dir');
  59. $this->smarty_template->cache_dir
  60. = Template::get_template_config($this->template_set_id, 'smarty_cache_dir');
  61. $this->smarty_template->config_dir
  62. = Template::get_template_config($this->template_set_id, 'smarty_config_dir');
  63. // note that we do not use Smarty's template_dir
  64. // because SquirrelMail has its own method of
  65. // determining template file paths
  66. //
  67. //$this->smarty_template->template_dir =
  68. }
  69. /**
  70. * Assigns values to template variables
  71. *
  72. * @param array|string $tpl_var the template variable name(s)
  73. * @param mixed $value the value to assign
  74. FIXME: Proposed idea to add a parameter here that turns variable
  75. encoding on, so that we can make sure output is always
  76. run through something like htmlspecialchars() (maybe even nl2br()?)
  77. *
  78. */
  79. function assign($tpl_var, $value = NULL) {
  80. $this->smarty_template->assign($tpl_var, $value);
  81. }
  82. /**
  83. * Assigns values to template variables by reference
  84. *
  85. * @param string $tpl_var the template variable name
  86. * @param mixed $value the referenced value to assign
  87. FIXME: Proposed idea to add a parameter here that turns variable
  88. encoding on, so that we can make sure output is always
  89. run through something like htmlspecialchars() (maybe even nl2br()?)
  90. *
  91. */
  92. function assign_by_ref($tpl_var, &$value) {
  93. $this->smarty_template->assign_by_ref($tpl_var, $value);
  94. }
  95. /**
  96. * Clears the values of all assigned varaiables.
  97. *
  98. */
  99. function clear_all_assign() {
  100. $this->smarty_template->clear_all_assign();
  101. }
  102. /**
  103. * Returns assigned variable value(s).
  104. *
  105. * @param string $varname If given, the value of that variable
  106. * is returned, assuming it has been
  107. * previously assigned. If not specified
  108. * an array of all assigned variables is
  109. * returned. (optional)
  110. *
  111. * @return mixed Desired single variable value or list of all
  112. * assigned variable values.
  113. *
  114. */
  115. function get_template_vars($varname=NULL) {
  116. return $this->smarty_template->get_template_vars($varname);
  117. }
  118. /**
  119. * Appends values to template variables
  120. *
  121. * @param array|string $tpl_var the template variable name(s)
  122. * @param mixed $value the value to append
  123. * @param boolean $merge when $value is given as an array,
  124. * this indicates whether or not that
  125. * array itself should be appended as
  126. * a new template variable value or if
  127. * that array's values should be merged
  128. * into the existing array of template
  129. * variable values
  130. FIXME: Proposed idea to add a parameter here that turns variable
  131. encoding on, so that we can make sure output is always
  132. run through something like htmlspecialchars() (maybe even nl2br()?)
  133. *
  134. */
  135. function append($tpl_var, $value = NULL, $merge = FALSE) {
  136. $this->smarty_template->append($tpl_var, $value, $merge);
  137. }
  138. /**
  139. * Appends values to template variables by reference
  140. *
  141. * @param string $tpl_var the template variable name
  142. * @param mixed $value the referenced value to append
  143. * @param boolean $merge when $value is given as an array,
  144. * this indicates whether or not that
  145. * array itself should be appended as
  146. * a new template variable value or if
  147. * that array's values should be merged
  148. * into the existing array of template
  149. * variable values
  150. FIXME: Proposed idea to add a parameter here that turns variable
  151. encoding on, so that we can make sure output is always
  152. run through something like htmlspecialchars() (maybe even nl2br()?)
  153. *
  154. */
  155. function append_by_ref($tpl_var, &$value, $merge = FALSE) {
  156. $this->smarty_template->append_by_ref($tpl_var, $value, $merge);
  157. }
  158. /**
  159. * Applys the template and generates final output destined
  160. * for the user's browser
  161. *
  162. * @param string $filepath The full file path to the template to be applied
  163. *
  164. * @return string The output for the given template
  165. *
  166. */
  167. function apply_template($filepath) {
  168. // if being passed a raw .css or .js file, default
  169. // Smarty delimiters will cause errors
  170. //
  171. if (strrpos($filepath, '.css') === (strlen($filepath) - 4)
  172. || strrpos($filepath, '.js') === (strlen($filepath) - 3)) {
  173. $this->smarty_template->left_delimiter = '{=';
  174. $this->smarty_template->right_delimiter = '=}';
  175. }
  176. // Smarty wants absolute paths
  177. //
  178. if (strpos($filepath, '/') === 0)
  179. return $this->smarty_template->fetch('file:' . $filepath);
  180. else
  181. return $this->smarty_template->fetch('file:' . getcwd() . '/' . $filepath);
  182. }
  183. }