4 * Smarty_Template.class.php
6 * This file contains a Template subclass intended as a bridge between
7 * SquirrelMail and Smarty. All abstract methods from the Template class
8 * are implemented here.
10 * @copyright © 2003-2007 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
14 * @subpackage Template
20 * The SquirrelMail Smarty Template class. Extends the base
21 * Template class for use with Smarty template pages.
23 * @author Paul Lesniewski <paul at squirrelmail.org>
24 * @package squirrelmail
27 class Smarty_Template
extends Template
31 * The Smarty template object
36 var $smarty_template = null;
42 * Please do not call directly. Use Template::construct_template().
44 * @param string $template_id the template ID
47 function Smarty_Template($template_id) {
48 //FIXME: find a way to test that this is ONLY ever called
49 // from parent's construct_template() method (I doubt it
50 // is worth the trouble to parse the current stack trace)
52 // trigger_error('Please do not use default Smarty_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
54 parent
::Template($template_id);
57 // load smarty settings
59 // instantiate and set up Smarty object
62 = Template
::get_template_config($this->template_set_id
, 'smarty_path');
63 require($smarty_path);
64 $this->smarty_template
= new Smarty();
65 $this->smarty_template
->compile_dir
66 = Template
::get_template_config($this->template_set_id
, 'smarty_compile_dir');
67 $this->smarty_template
->cache_dir
68 = Template
::get_template_config($this->template_set_id
, 'smarty_cache_dir');
69 $this->smarty_template
->config_dir
70 = Template
::get_template_config($this->template_set_id
, 'smarty_config_dir');
72 // note that we do not use Smarty's template_dir
73 // because SquirrelMail has its own method of
74 // determining template file paths
76 //$this->smarty_template->template_dir =
81 * Assigns values to template variables
83 * @param array|string $tpl_var the template variable name(s)
84 * @param mixed $value the value to assign
85 FIXME: Proposed idea to add a parameter here that turns variable
86 encoding on, so that we can make sure output is always
87 run through something like htmlspecialchars() (maybe even nl2br()?)
90 function assign($tpl_var, $value = NULL) {
92 $this->smarty_template
->assign($tpl_var, $value);
97 * Assigns values to template variables by reference
99 * @param string $tpl_var the template variable name
100 * @param mixed $value the referenced value to assign
101 FIXME: Proposed idea to add a parameter here that turns variable
102 encoding on, so that we can make sure output is always
103 run through something like htmlspecialchars() (maybe even nl2br()?)
106 function assign_by_ref($tpl_var, &$value) {
108 $this->smarty_template
->assign_by_ref($tpl_var, $value);
113 * Clears the values of all assigned varaiables.
116 function clear_all_assign() {
118 $this->smarty_template
->clear_all_assign();
123 * Returns assigned variable value(s).
125 * @param string $varname If given, the value of that variable
126 * is returned, assuming it has been
127 * previously assigned. If not specified
128 * an array of all assigned variables is
129 * returned. (optional)
131 * @return mixed Desired single variable value or list of all
132 * assigned variable values.
135 function get_template_vars($varname=NULL) {
137 return $this->smarty_template
->get_template_vars($varname);
142 * Appends values to template variables
144 * @param array|string $tpl_var the template variable name(s)
145 * @param mixed $value the value to append
146 * @param boolean $merge when $value is given as an array,
147 * this indicates whether or not that
148 * array itself should be appended as
149 * a new template variable value or if
150 * that array's values should be merged
151 * into the existing array of template
153 FIXME: Proposed idea to add a parameter here that turns variable
154 encoding on, so that we can make sure output is always
155 run through something like htmlspecialchars() (maybe even nl2br()?)
158 function append($tpl_var, $value = NULL, $merge = FALSE) {
160 $this->smarty_template
->append($tpl_var, $value, $merge);
165 * Appends values to template variables by reference
167 * @param string $tpl_var the template variable name
168 * @param mixed $value the referenced value to append
169 * @param boolean $merge when $value is given as an array,
170 * this indicates whether or not that
171 * array itself should be appended as
172 * a new template variable value or if
173 * that array's values should be merged
174 * into the existing array of template
176 FIXME: Proposed idea to add a parameter here that turns variable
177 encoding on, so that we can make sure output is always
178 run through something like htmlspecialchars() (maybe even nl2br()?)
181 function append_by_ref($tpl_var, &$value, $merge = FALSE) {
183 $this->smarty_template
->append_by_ref($tpl_var, $value, $merge);
188 * Applys the template and generates final output destined
189 * for the user's browser
191 * @param string $filepath The full file path to the template to be applied
193 * @return string The output for the given template
196 function apply_template($filepath) {
198 // if being passed a raw .css or .js file, default
199 // Smarty delimiters will cause errors
201 if (strrpos($filepath, '.css') === (strlen($filepath) - 4)
202 ||
strrpos($filepath, '.js') === (strlen($filepath) - 3)) {
203 $this->smarty_template
->left_delimiter
= '{=';
204 $this->smarty_template
->right_delimiter
= '=}';
207 // Smarty wants absolute paths
209 if (strpos($filepath, '/') === 0)
210 return $this->smarty_template
->fetch('file:' . $filepath);
212 return $this->smarty_template
->fetch('file:' . getcwd() . '/' . $filepath);