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
19 * The SquirrelMail (Foowd) template class.
21 * Basic template class for capturing values and pluging them into a template.
22 * This class uses a similar API to Smarty.
25 * @author Monte Ohrt <monte at ispi.net>
26 * @author Andrei Zmievski <andrei at php.net>
27 * @package squirrelmail
32 * The templates values array
36 var $values = array();
39 * The template directory to use
43 var $template_dir = '';
46 * Template files provided by this template set
50 var $templates_provided = array();
53 * Javascript files required by the template
57 var $required_js_files = array();
60 * Javascript files provided by the template. If a JS file is required, but
61 * not provided, the js file by the same name will be included from the
62 * default template directory.
66 var $provided_js_files = array();
69 * Additional stylesheets provided by the template. This allows template
70 * authors to provide additional CSS sheets to templates while using the
71 * default template set stylesheet for other definitions.
73 var $additional_css_sheets = array();
78 * @param string $sTplDir where the template set is located
80 function Template($sTplDir) {
81 $this->template_dir
= $sTplDir;
83 // Pull in the tempalte config file
84 if (!file_exists($this->template_dir
. 'template.php')) {
85 trigger_error('No template.php could be found in the requested template directory ("'.$this->template_dir
.'")', E_USER_ERROR
);
87 include ($this->template_dir
. 'template.php');
88 $this->templates_provided
= is_array($templates_provided) ?
$templates_provided : array();
89 $this->required_js_files
= is_array($required_js_files) ?
$required_js_files : array();
90 $this->provided_js_files
= is_array($provided_js_files) ?
$provided_js_files: array();
91 $this->additional_css_sheets
= is_array($additional_css_sheets) ?
$additional_css_sheets : array();
97 * Assigns values to template variables
99 * @param array|string $tpl_var the template variable name(s)
100 * @param mixed $value the value to assign
102 function assign($tpl_var, $value = NULL) {
103 if (is_array($tpl_var))
105 foreach ($tpl_var as $key => $val)
108 $this->values
[$key] = $val;
114 $this->values
[$tpl_var] = $value;
119 * Assigns values to template variables by reference
121 * @param string $tpl_var the template variable name
122 * @param mixed $value the referenced value to assign
124 function assign_by_ref($tpl_var, &$value)
127 $this->values
[$tpl_var] = &$value;
131 * Appends values to template variables
133 * @param array|string $tpl_var the template variable name(s)
134 * @param mixed $value the value to append
135 * @param boolean $merge when $value is given as an array,
136 * this indicates whether or not that
137 * array itself should be appended as
138 * a new template variable value or if
139 * that array's values should be merged
140 * into the existing array of template
143 function append($tpl_var, $value = NULL, $merge = FALSE)
145 if (is_array($tpl_var))
147 //FIXME: $tpl_var is supposed to be a list of template var names,
148 // so we should be looking at the values NOT the keys!
149 foreach ($tpl_var as $_key => $_val)
153 if(isset($this->values
[$_key]) && !is_array($this->values
[$_key]))
154 settype($this->values
[$_key],'array');
156 //FIXME: we should be iterating the $value array here not the values of the
157 // list of template variable names! I think this is totally broken
158 // This might just be a matter of needing to clarify the method's API;
159 // values may have been meant to be passed in $tpl_var in the case that
160 // $tpl_var is an array. Ugly and non-intuitive.
161 // PROPOSAL: API should be as such:
162 // if (is_string($tpl_var)) then $values are added/merged as already done
163 // if (is_array($tpl_var)) then $values is required to be an array whose
164 // keys must match up with $tpl_var keys and
165 // whose values are then what is added to
166 // each template variable value (array or
167 // strings, doesn't matter)
168 if($merge && is_array($_val))
170 foreach($_val as $_mkey => $_mval)
171 $this->values
[$_key][$_mkey] = $_mval;
174 $this->values
[$_key][] = $_val;
180 if ($tpl_var != '' && isset($value))
182 if(isset($this->values
[$tpl_var]) && !is_array($this->values
[$tpl_var]))
183 settype($this->values
[$tpl_var],'array');
185 if($merge && is_array($value))
187 foreach($value as $_mkey => $_mval)
188 $this->values
[$tpl_var][$_mkey] = $_mval;
191 $this->values
[$tpl_var][] = $value;
197 * Appends values to template variables by reference
199 * @param string $tpl_var the template variable name
200 * @param mixed $value the referenced value to append
201 * @param boolean $merge when $value is given as an array,
202 * this indicates whether or not that
203 * array itself should be appended as
204 * a new template variable value or if
205 * that array's values should be merged
206 * into the existing array of template
209 function append_by_ref($tpl_var, &$value, $merge = FALSE)
211 if ($tpl_var != '' && isset($value))
213 if(!@is_array
($this->values
[$tpl_var]))
214 settype($this->values
[$tpl_var],'array');
216 if ($merge && is_array($value))
218 foreach($value as $_key => $_val)
219 $this->values
[$tpl_var][$_key] = &$value[$_key];
222 $this->values
[$tpl_var][] = &$value;
227 * Display the template
229 * @param string $file The template file to use
231 function display($file)
233 // Pull in our config file
234 $t = &$this->values
; // place values array directly in scope
236 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
237 if (!file_exists($template)) {
238 trigger_error('The template "'.htmlspecialchars($file).'" could not be displayed!', E_USER_ERROR
);
247 * Return the results of applying a template.
249 * @param string $file The template file to use
250 * @return string A string of the results
252 function fetch($file) {
253 $t = &$this->values
; // place values array directly in scope
255 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
256 if (!file_exists($template)) {
257 trigger_error('The template "'.htmlspecialchars($file).'" could not be fetched!', E_USER_ERROR
);
261 $contents = ob_get_contents();
268 * Return paths to the required javascript files. Used when generating page
271 * @return array $paths
273 function getJavascriptIncludes () {
275 foreach ($this->required_js_files
as $file) {
276 if (in_array($file, $this->provided_js_files
))
277 $paths[] = './'.$this->template_dir
.'js/'.basename($file);
278 else $paths[] = SM_PATH
.'templates/default/js/'.basename($file);
285 * Return any additional stylsheets provided by the template. Used when
286 * generating page headers.
288 * @return array $paths
290 function getAdditionalStyleSheets () {
292 foreach ($this->additional_css_sheets
as $css) {
293 $css = basename($css);
294 if (strtolower($css) == 'stylesheet.tpl') {