238d0d1379ae8f05881985de5f44c0170f26d564
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 (namely me to begin with :p) to provide additional CSS sheets
71 * to templates while using the default template set stylesheet for other
74 var $additional_css_sheets = array();
79 * @param string $sTplDir where the template set is located
81 function Template($sTplDir) {
82 $this->template_dir
= $sTplDir;
84 // Pull in the tempalte config file
85 if (!file_exists($this->template_dir
. 'template.php')) {
86 trigger_error('No template.php could be found in the requested template directory ("'.$this->template_dir
.'")', E_USER_ERROR
);
88 include ($this->template_dir
. 'template.php');
89 $this->templates_provided
= is_array($templates_provided) ?
$templates_provided : array();
90 $this->required_js_files
= is_array($required_js_files) ?
$required_js_files : array();
91 $this->provided_js_files
= is_array($provided_js_files) ?
$provided_js_files: array();
92 $this->additional_css_sheets
= is_array($additional_css_sheets) ?
$additional_css_sheets : array();
98 * Assigns values to template variables
100 * @param array|string $tpl_var the template variable name(s)
101 * @param mixed $value the value to assign
103 function assign($tpl_var, $value = NULL) {
104 if (is_array($tpl_var))
106 foreach ($tpl_var as $key => $val)
109 $this->values
[$key] = $val;
115 $this->values
[$tpl_var] = $value;
120 * Assigns values to template variables by reference
122 * @param string $tpl_var the template variable name
123 * @param mixed $value the referenced value to assign
125 function assign_by_ref($tpl_var, &$value)
128 $this->values
[$tpl_var] = &$value;
132 * Appends values to template variables
134 * @param array|string $tpl_var the template variable name(s)
135 * @param mixed $value the value to append
137 function append($tpl_var, $value = NULL, $merge = FALSE)
139 if (is_array($tpl_var))
141 foreach ($tpl_var as $_key => $_val)
145 if(isset($this->values
[$_key]) && !is_array($this->values
[$_key]))
146 settype($this->values
[$_key],'array');
148 if($merge && is_array($_val))
150 foreach($_val as $_mkey => $_mval)
151 $this->values
[$_key][$_mkey] = $_mval;
154 $this->values
[$_key][] = $_val;
160 if ($tpl_var != '' && isset($value))
162 if(isset($this->values
[$tpl_var]) && !is_array($this->values
[$tpl_var]))
163 settype($this->values
[$tpl_var],'array');
165 if($merge && is_array($value))
167 foreach($value as $_mkey => $_mval)
168 $this->values
[$tpl_var][$_mkey] = $_mval;
171 $this->values
[$tpl_var][] = $value;
177 * Appends values to template variables by reference
179 * @param string $tpl_var the template variable name
180 * @param mixed $value the referenced value to append
182 function append_by_ref($tpl_var, &$value, $merge = FALSE)
184 if ($tpl_var != '' && isset($value))
186 if(!@is_array
($this->values
[$tpl_var]))
187 settype($this->values
[$tpl_var],'array');
189 if ($merge && is_array($value))
191 foreach($value as $_key => $_val)
192 $this->values
[$tpl_var][$_key] = &$value[$_key];
195 $this->values
[$tpl_var][] = &$value;
200 * Display the template
202 * @param string $file The template file to use
204 function display($file)
207 * We want to make sure that certain variables are always passed to the
208 * templates b/c they are critical to certain template functions.
210 $always_include = array('icon_theme_path', 'sTplDir');
211 foreach ($always_include as $var) {
212 if (!isset($this->values
[$var]) && isset($GLOBALS[$var])) {
213 $this->assign($var, $GLOBALS[$var]);
217 // Pull in our config file
218 $t = &$this->values
; // place values array directly in scope
220 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
221 if (!file_exists($template)) {
222 trigger_error('The template "'.htmlspecialchars($file).'" could not be displayed!', E_USER_ERROR
);
231 * Return the results of applying a template.
233 * @param string $file The template file to use
234 * @return string A string of the results
236 function fetch($file) {
237 $t = &$this->values
; // place values array directly in scope
239 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
240 if (!file_exists($template)) {
241 trigger_error('The template "'.htmlspecialchars($file).'" could not be fetched!', E_USER_ERROR
);
245 $contents = ob_get_contents();
252 * Return paths to the required javascript files. Used when generating page
255 * @return array $paths
257 function getJavascriptIncludes () {
259 foreach ($this->required_js_files
as $file) {
260 if (in_array($file, $this->provided_js_files
))
261 $paths[] = './'.$this->template_dir
.'js/'.basename($file);
262 else $paths[] = SM_PATH
.'templates/default/js/'.basename($file);
269 * Return any additional stylsheets provided by the template. Used when
270 * generating page headers.
272 * @return array $paths
274 function getAdditionalStyleSheets () {
276 foreach ($this->additional_css_sheets
as $css) {
277 $css = basename($css);
278 if (strtolower($css) == 'stylesheet.tpl') {