e09e53116337170b0cf972da99cef64950736638
4 * Copyright 2003, Paul James
6 * This file contains some methods from the Smarty templating engine version
7 * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>.
9 * The SquirrelMail (Foowd) template implementation.
10 * Derived from the foowd template implementation and adapted
12 * @copyright © 2005-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
15 * @package squirrelmail
20 * The SquirrelMail PHP Template class. Extends the base
21 * Template class for use with PHP template pages.
24 * @author Monte Ohrt <monte at ispi.net>
25 * @author Andrei Zmievski <andrei at php.net>
26 * @author Paul Lesniewski
27 * @package squirrelmail
30 class PHP_Template
extends Template
34 * The templates values array
39 var $values = array();
45 * Please do not call directly. Use Template::construct_template().
47 * @param string $template_id the template ID
50 function PHP_Template($template_id) {
51 //FIXME: find a way to test that this is ONLY ever called
52 // from parent's construct_template() method (I doubt it
53 // is worth the trouble to parse the current stack trace)
55 // trigger_error('Please do not use default PHP_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
57 parent
::Template($template_id);
62 * Assigns values to template variables
64 * @param array|string $tpl_var the template variable name(s)
65 * @param mixed $value the value to assign
66 FIXME: Proposed idea to add a parameter here that turns variable
67 encoding on, so that we can make sure output is always
68 run through something like htmlspecialchars() (maybe even nl2br()?)
71 function assign($tpl_var, $value = NULL) {
73 if (is_array($tpl_var))
75 foreach ($tpl_var as $key => $val)
78 $this->values
[$key] = $val;
84 $this->values
[$tpl_var] = $value;
90 * Assigns values to template variables by reference
92 * @param string $tpl_var the template variable name
93 * @param mixed $value the referenced value to assign
94 FIXME: Proposed idea to add a parameter here that turns variable
95 encoding on, so that we can make sure output is always
96 run through something like htmlspecialchars() (maybe even nl2br()?)
99 function assign_by_ref($tpl_var, &$value) {
102 $this->values
[$tpl_var] = &$value;
107 * Clears the values of all assigned varaiables.
110 function clear_all_assign() {
112 $this->values
= array();
117 * Returns assigned variable value(s).
119 * @param string $varname If given, the value of that variable
120 * is returned, assuming it has been
121 * previously assigned. If not specified
122 * an array of all assigned variables is
123 * returned. (optional)
125 * @return mixed Desired single variable value or list of all
126 * assigned variable values.
129 function get_template_vars($varname=NULL) {
131 // just looking for one value
133 if (!empty($varname)) {
134 if (!empty($this->values
[$varname]))
135 return $this->values
[$varname];
137 // FIXME: this OK? What does Smarty do?
142 // return all variable values
144 return $this->values
;
149 * Appends values to template variables
151 * @param array|string $tpl_var the template variable name(s)
152 * @param mixed $value the value to append
153 * @param boolean $merge when $value is given as an array,
154 * this indicates whether or not that
155 * array itself should be appended as
156 * a new template variable value or if
157 * that array's values should be merged
158 * into the existing array of template
160 FIXME: Proposed idea to add a parameter here that turns variable
161 encoding on, so that we can make sure output is always
162 run through something like htmlspecialchars() (maybe even nl2br()?)
165 function append($tpl_var, $value = NULL, $merge = FALSE)
167 if (is_array($tpl_var))
169 foreach ($tpl_var as $_key => $_val)
173 if(isset($this->values
[$_key]) && !is_array($this->values
[$_key]))
174 settype($this->values
[$_key],'array');
176 if($merge && is_array($_val))
178 // FIXME: Tentative testing seems to indicate that
179 // this does not mirror Smarty behavior; Smarty
180 // seems to append the full array as a new element
181 // instead of merging, so this behavior is technically
182 // more "correct", but Smarty seems to differ
183 foreach($_val as $_mkey => $_mval)
184 $this->values
[$_key][$_mkey] = $_mval;
187 $this->values
[$_key][] = $_val;
193 if ($tpl_var != '' && isset($value))
195 if(isset($this->values
[$tpl_var]) && !is_array($this->values
[$tpl_var]))
196 settype($this->values
[$tpl_var],'array');
198 if($merge && is_array($value))
200 foreach($value as $_mkey => $_mval)
201 $this->values
[$tpl_var][$_mkey] = $_mval;
204 $this->values
[$tpl_var][] = $value;
210 * Appends values to template variables by reference
212 * @param string $tpl_var the template variable name
213 * @param mixed $value the referenced value to append
214 * @param boolean $merge when $value is given as an array,
215 * this indicates whether or not that
216 * array itself should be appended as
217 * a new template variable value or if
218 * that array's values should be merged
219 * into the existing array of template
221 FIXME: Proposed idea to add a parameter here that turns variable
222 encoding on, so that we can make sure output is always
223 run through something like htmlspecialchars() (maybe even nl2br()?)
226 function append_by_ref($tpl_var, &$value, $merge = FALSE)
228 if ($tpl_var != '' && isset($value))
230 if(!@is_array
($this->values
[$tpl_var]))
231 settype($this->values
[$tpl_var],'array');
233 if ($merge && is_array($value))
235 foreach($value as $_key => $_val)
236 $this->values
[$tpl_var][$_key] = &$value[$_key];
239 $this->values
[$tpl_var][] = &$value;
244 * Applys the template and generates final output destined
245 * for the user's browser
247 * @param string $filepath The full file path to the template to be applied
249 * @return string The output for the given template
252 function apply_template($filepath) {
254 // place values array directly in scope
255 // ($t? let's try to be more verbose please :-) )
261 $contents = ob_get_contents();