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();
71 * @param string $sTplDir where the template set is located
73 function Template($sTplDir) {
74 $this->template_dir
= $sTplDir;
76 // Pull in the tempalte config file
77 include ($this->template_dir
. 'template.php');
78 $this->templates_provided
= is_array($templates_provided) ?
$templates_provided : array();
79 $this->required_js_files
= is_array($required_js_files) ?
$required_js_files : array();
80 $this->provided_js_files
= is_array($provided_js_files) ?
$provided_js_files: array();
82 # echo 'Template Dir: '.$this->template_dir.': ';
83 # var_dump($this->templates_provided);
88 * Assigns values to template variables
90 * @param array|string $tpl_var the template variable name(s)
91 * @param mixed $value the value to assign
93 function assign($tpl_var, $value = NULL) {
94 if (is_array($tpl_var))
96 foreach ($tpl_var as $key => $val)
99 $this->values
[$key] = $val;
105 $this->values
[$tpl_var] = $value;
110 * Assigns values to template variables by reference
112 * @param string $tpl_var the template variable name
113 * @param mixed $value the referenced value to assign
115 function assign_by_ref($tpl_var, &$value)
118 $this->values
[$tpl_var] = &$value;
122 * Appends values to template variables
124 * @param array|string $tpl_var the template variable name(s)
125 * @param mixed $value the value to append
127 function append($tpl_var, $value = NULL, $merge = FALSE)
129 if (is_array($tpl_var))
131 foreach ($tpl_var as $_key => $_val)
135 if(isset($this->values
[$_key]) && !is_array($this->values
[$_key]))
136 settype($this->values
[$_key],'array');
138 if($merge && is_array($_val))
140 foreach($_val as $_mkey => $_mval)
141 $this->values
[$_key][$_mkey] = $_mval;
144 $this->values
[$_key][] = $_val;
150 if ($tpl_var != '' && isset($value))
152 if(isset($this->values
[$tpl_var]) && !is_array($this->values
[$tpl_var]))
153 settype($this->values
[$tpl_var],'array');
155 if($merge && is_array($value))
157 foreach($value as $_mkey => $_mval)
158 $this->values
[$tpl_var][$_mkey] = $_mval;
161 $this->values
[$tpl_var][] = $value;
167 * Appends values to template variables by reference
169 * @param string $tpl_var the template variable name
170 * @param mixed $value the referenced value to append
172 function append_by_ref($tpl_var, &$value, $merge = FALSE)
174 if ($tpl_var != '' && isset($value))
176 if(!@is_array
($this->values
[$tpl_var]))
177 settype($this->values
[$tpl_var],'array');
179 if ($merge && is_array($value))
181 foreach($value as $_key => $_val)
182 $this->values
[$tpl_var][$_key] = &$value[$_key];
185 $this->values
[$tpl_var][] = &$value;
190 * Display the template
192 * @param string $file The template file to use
194 function display($file)
196 // Pull in our config file
197 $t = &$this->values
; // place values array directly in scope
199 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
206 * Return the results of applying a template.
208 * @param string $file The template file to use
209 * @return string A string of the results
211 function fetch($file)
214 $t = &$this->values
; // place values array directly in scope
216 $template = in_array($file, $this->templates_provided
) ?
$this->template_dir
. $file : SM_PATH
.'templates/default/'. $file;
218 $contents = ob_get_contents();
224 * Return paths to the required javascript files. Used when generating page
227 * @return array $paths
229 function getJavascriptIncludes () {
231 foreach ($this->required_js_files
as $file) {
232 if (in_array($file, $this->provided_js_files
))
233 $paths[] = './'.$this->template_dir
.'js/'.basename($file);
234 else $paths[] = SM_PATH
.'templates/default/js/'.basename($file);