Cgiapp2
[ class tree: Cgiapp2 ] [ index: Cgiapp2 ] [ all elements ]

Source for file Smarty.class.php

Documentation is available at Smarty.class.php

  1. <?php
  2. /**
  3. * Cgiapp2 - Framework for building reusable web-applications
  4. *
  5. * A PHP5 port of perl's CGI::Application, a framework for building reusable web
  6. * applications.
  7. *
  8. * @package Cgiapp2
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>; based on
  10. * CGI::Application, by Jesse Erlbaum <jesse@erlbaum.net>, et. al.
  11. * @copyright (c) 2004 - present, Matthew Weier O'Phinney
  12. * @license BSD License (http://www.opensource.org/licenses/bsd-license.php)
  13. * @category Tools and Utilities
  14. * @tutorial Cgiapp2/Cgiapp2.cls
  15. * @version $Id:$
  16. */
  17.  
  18. /**
  19. * Cgiapp2 plugin
  20. */
  21. require_once 'Cgiapp2.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Plugin_Template_Interface
  25. */
  26. require_once 'Cgiapp2/Plugin/Template/Interface.class.php';
  27.  
  28. /**
  29. * Smarty Template Plugin for Cgiapp2
  30. *
  31. * Implements {@link Cgiapp2_Plugin_Template_Interface} to create a Smarty plugin
  32. * for {@link Cgiapp2}.
  33. *
  34. * Additionally, Cgiapp2_Plugin_Smarty implements a singleton and dynamic proxy.
  35. * This allows you to do the following:
  36. * <code>
  37. * // Register rawurlencode() with Smarty engine.
  38. * Cgiapp2_Plugin_Smarty::getInstance()->register_modifier('rawurlencode', 'rawurlencode');
  39. * </code>
  40. *
  41. * Registers with Cgiapp2's tmpl_path, tmpl_assign, and tmpl_fetch hooks;
  42. * registration is done with the Cgiapp2 class.
  43. *
  44. * @package Cgiapp2
  45. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  46. * @version @release-version@
  47. */
  48. class Cgiapp2_Plugin_Smarty implements Cgiapp2_Plugin_Template_Interface
  49. {
  50. /**
  51. * Path to template storage root
  52. *
  53. * @var string
  54. * @access protected
  55. */
  56. protected $tmpl_path;
  57.  
  58. /**
  59. * Hold template instance
  60. *
  61. * @var string
  62. * @access private
  63. * @static
  64. */
  65. private static $instance = false;
  66.  
  67. /**
  68. * Smarty instance
  69. *
  70. * @var object
  71. * @access protected
  72. */
  73. protected $t;
  74.  
  75. /**
  76. * Constructor
  77. *
  78. * Creates an instance and initializes the {@link $tmpl_path} property.
  79. *
  80. * In addition, the constructor registers a function 'sslash', with Smarty,
  81. * which allows you to use stripslashes() directly in your template:
  82. * <code>
  83. * {* stripslashes($variable) *}
  84. * {$variable|sslash}
  85. * </code>
  86. *
  87. * @param mixed $tmpl_path
  88. * @param mixed $extra_params
  89. * @access private
  90. * @return void
  91. */
  92. private function __construct($tmpl_path, $extra_params)
  93. {
  94. $smarty = new Smarty;
  95.  
  96. // Register stripslashes() with Smarty as 'sslash'
  97. $smarty->register_modifier("sslash", "stripslashes");
  98.  
  99. // Initialize extra parameters, if given
  100. if (isset($extra_params) && Cgiapp2::is_assoc_array($extra_params))
  101. {
  102. foreach ($extra_params as $param => $value) {
  103. $smarty->$param = $value;
  104. }
  105. }
  106.  
  107. $this->t = $smarty;
  108. $this->setTmplPath($tmpl_path);
  109. }
  110.  
  111. /**
  112. * Dynamic proxy for Smarty methods
  113. *
  114. * Allows calling Smarty methods as part of the Cgiapp2_Plugin_Smarty object.
  115. * Since the class also implements the singleton pattern, you may easily
  116. * call Smarty methods from anywhere:
  117. * <code>
  118. * Cgiapp2_Plugin_Smarty::getInstance()->register_modifier('sslash', 'stripslashes');
  119. * </code>
  120. *
  121. * @param mixed $method
  122. * @param mixed $args
  123. * @access public
  124. * @return void
  125. */
  126. public function __call($method, $args)
  127. {
  128. if (method_exists($this->t, $method)) {
  129. return call_user_func_array(array($this->t, $method), $args);
  130. }
  131. }
  132.  
  133. /**
  134. * Proxy: retrieve Smarty property values
  135. *
  136. * @access public
  137. * @param string $key
  138. * @return mixed
  139. */
  140. public function __get($key)
  141. {
  142. if (isset($this->t->$key)) {
  143. return $this->t->$key;
  144. }
  145.  
  146. return;
  147. }
  148.  
  149. /**
  150. * Proxy: set Smarty property values
  151. *
  152. * @access public
  153. * @param mixed $key
  154. * @param mixed $val
  155. * @return void
  156. */
  157. public function __set($key, $val)
  158. {
  159. $this->t->$key = $val;
  160.  
  161. return;
  162. }
  163.  
  164. /**
  165. * Singleton
  166. *
  167. * Returns false if unable to find instance, Smarty, or missing arguments.
  168. * You can access this at any time:
  169. * <code>
  170. * $tpl = Cgiapp2_Plugin_Smarty::getInstance();
  171. * </code>
  172. *
  173. * @static
  174. * @access public
  175. * @param mixed $cgiapp Cgiapp2 instance object
  176. * @param mixed $tmpl_path Path to template root directory
  177. * @param mixed $extra_params Optional; extra parameters with which to
  178. * initialize Smarty
  179. * @return void
  180. * @throws Exception if unable to insantiate Smarty
  181. */
  182. public static function getInstance()
  183. {
  184. // Return instance if it exists already
  185. if (false !== ($instance = self::$instance)) {
  186. return $instance;
  187. }
  188.  
  189. // Get arguments and any extra params
  190. $args = func_get_args();
  191. if (2 > count($args)) {
  192. throw new Exception('No instance found, and missing arguments to create one');
  193. return false;
  194. }
  195. $extra_params = null;
  196. $cgiapp = array_shift($args);
  197. $tmpl_path = array_shift($args);
  198. if (0 < count($args)) {
  199. $extra_params = array_shift($args);
  200. }
  201.  
  202. // Set smarty path and include Smarty class
  203. if (false !== ($SMARTY_PATH = $cgiapp->param('SMARTY_PATH'))) {
  204. $PATH = ini_get('include_path');
  205. ini_set('include_path', $SMARTY_PATH . ':' . $PATH);
  206. }
  207. @include_once 'Smarty.class.php';
  208. if (!class_exists('Smarty')) {
  209. throw new Exception('Smarty not found');
  210. return false;
  211. }
  212.  
  213. self::$instance = new Cgiapp2_Plugin_Smarty($tmpl_path, $extra_params);
  214. return self::$instance;
  215. }
  216.  
  217. /**
  218. * Set the template path
  219. *
  220. * Uses $tmpl_path to set Smarty's template_dir, compile_dir, cache_dir, and
  221. * config_dir properties.
  222. *
  223. * @access public
  224. * @param mixed $tmpl_path
  225. * @return void
  226. */
  227. public function setTmplPath($tmpl_path)
  228. {
  229. $this->tmpl_path = $tmpl_path;
  230.  
  231. $this->t->template_dir = "$tmpl_path/templates/";
  232. $this->t->compile_dir = "$tmpl_path/templates_c/";
  233. $this->t->cache_dir = "$tmpl_path/cache/";
  234. $this->t->config_dir = "$tmpl_path/configs/";
  235. }
  236.  
  237. /**
  238. * Initialize a template instance and/or set the template path
  239. *
  240. * If no Cgiapp2_Plugin_Smarty instance currently exists, it is first
  241. * initialized via {@link getInstance()} using the template path, extra
  242. * parameters, and Cgiapp2-based object instance. Smarty is looked for in the
  243. * Cgiapp2 instance parameter 'SMARTY_PATH'; if not found, or an error occurs
  244. * initializing Smarty, a warning is raised.
  245. *
  246. * init() is used by Smarty to set the template root path to each of the
  247. * required Smarty template directories (templates, templates_c, configs,
  248. * and cache). If the template path has not changed, nothing is done.
  249. *
  250. * @static
  251. * @access public
  252. * @param mixed $cgiapp
  253. * @param mixed $tmpl_path
  254. * @param mixed $extra_params
  255. * @return bool
  256. */
  257. public static function init(Cgiapp2 $cgiapp, $tmpl_path, $extra_params = null)
  258. {
  259. // Get smarty object
  260. $instance = self::getInstance($cgiapp, $tmpl_path, $extra_params);
  261.  
  262. if ($instance->tmpl_path != $tmpl_path) {
  263. $instance->setTmplPath($tmpl_path);
  264. }
  265.  
  266. return true;
  267. }
  268.  
  269. /**
  270. * Assign a variable or variables to a template
  271. *
  272. * assign() can be used to assign data to a template. Internally, it calls
  273. * Smarty's assign() method.
  274. *
  275. * You can also send it an associative array of variable names => values,
  276. * and all elements included will be sent to the template.
  277. *
  278. * @static
  279. * @access public
  280. * @return bool
  281. * @throws Exception when bad data passed
  282. */
  283. public static function assign(Cgiapp2 $cgiapp)
  284. {
  285. $args = func_get_args();
  286. $cgiapp = array_shift($args);
  287. $argc = count($args);
  288.  
  289. if (1 == $argc) {
  290. $values = array_shift($args);
  291. if (Cgiapp2::is_assoc_array($values)) {
  292. self::getInstance()->t->assign($values);
  293. } else {
  294. throw new Exception('Bad array passed to Cgiapp2_Plugin_Smarty::assign()');
  295. return false;
  296. }
  297. } elseif (2 == $argc) {
  298. $key = array_shift($args);
  299. if (is_string($key)) {
  300. $val = array_shift($args);
  301. self::getInstance()->t->assign(trim($key), $val);
  302. } else {
  303. throw new Exception('Attempting to assign non-string key');
  304. return false;
  305. }
  306. } else {
  307. throw new Exception('Bad number or type of arguments passed to Cgiapp2_Plugin_Smarty::assign()');
  308. return false;
  309. }
  310.  
  311. return true;
  312. }
  313.  
  314. /**
  315. * Fetch template contents
  316. *
  317. * If not template file is provided, returns an empty string. If unable to
  318. * locate a Cgiapp2_Plugin_Smarty instance, returns an empty string and
  319. * raises a warning.
  320. *
  321. * Internally, calls Smarty's fetch method:
  322. * <code>
  323. * $smarty->fetch('some.tpl');
  324. * </code>
  325. *
  326. * @static
  327. * @access public
  328. * @param mixed $cgiapp
  329. * @param mixed $tmpl_file
  330. * @return string
  331. */
  332. public static function fetch(Cgiapp2 $cgiapp, $tmpl_file)
  333. {
  334. if (empty($tmpl_file) || !is_string($tmpl_file)) {
  335. return '';
  336. }
  337.  
  338. $output = self::getInstance()->t->fetch($tmpl_file);
  339. return $output;
  340. }
  341. }
  342.  
  343. /**
  344. * Register callbacks with tmpl_path, tmpl_assign, and tmpl_fetch callback hooks
  345. * of Cgiapp2
  346. */
  347. Cgiapp2::add_callback('tmpl_path', array('Cgiapp2_Plugin_Smarty', 'init'), 'Cgiapp2');
  348. Cgiapp2::add_callback('tmpl_assign', array('Cgiapp2_Plugin_Smarty', 'assign'), 'Cgiapp2');
  349. Cgiapp2::add_callback('tmpl_fetch', array('Cgiapp2_Plugin_Smarty', 'fetch'), 'Cgiapp2');
  350. ?>

Documentation generated on Sat, 03 Jun 2006 10:48:56 -0400 by phpDocumentor 1.3.0RC5