Added ability for template authors to include additional stylesheets. This allows...
[squirrelmail.git] / class / template / template.class.php
CommitLineData
94b47c30 1<?php
4b4abf93 2
94b47c30 3/**
4 * Copyright 2003, Paul James
94b47c30 5 *
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>.
8 *
94b47c30 9 * The SquirrelMail (Foowd) template implementation.
10 * Derived from the foowd template implementation and adapted
11 * for squirrelmail
47ccfad4 12 * @copyright &copy; 2005-2006 The SquirrelMail Project Team
4b4abf93 13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9f7d2fa9 14 * @version $Id$
e045ce66 15 * @package squirrelmail
94b47c30 16 */
17
18/**
19 * The SquirrelMail (Foowd) template class.
20 *
21 * Basic template class for capturing values and pluging them into a template.
22 * This class uses a similar API to Smarty.
23 *
24 * @author Paul James
4b4abf93 25 * @author Monte Ohrt <monte at ispi.net>
26 * @author Andrei Zmievski <andrei at php.net>
9f7d2fa9 27 * @package squirrelmail
94b47c30 28 */
29class Template
30{
31 /**
32 * The templates values array
33 *
34 * @var array
35 */
36 var $values = array();
37
38 /**
39 * The template directory to use
40 *
41 * @var string
42 */
49ea2d20 43 var $template_dir = '';
94b47c30 44
44452136 45 /**
46 * Template files provided by this template set
47 *
48 * @var array
49 */
50 var $templates_provided = array();
51
52 /**
53 * Javascript files required by the template
54 *
55 * @var array
56 */
57 var $required_js_files = array();
58
59 /**
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.
63 *
64 * @var array
65 */
66 var $provided_js_files = array();
67
3cecf1cd 68 /**
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
72 * definitions.
73 */
74 var $additional_css_sheets = array();
75
94b47c30 76 /**
77 * Constructor
78 *
79 * @param string $sTplDir where the template set is located
80 */
49ea2d20 81 function Template($sTplDir) {
94b47c30 82 $this->template_dir = $sTplDir;
44452136 83
84 // Pull in the tempalte config file
85 include ($this->template_dir . 'template.php');
86 $this->templates_provided = is_array($templates_provided) ? $templates_provided : array();
87 $this->required_js_files = is_array($required_js_files) ? $required_js_files : array();
88 $this->provided_js_files = is_array($provided_js_files) ? $provided_js_files: array();
3cecf1cd 89 $this->additional_css_sheets = is_array($additional_css_sheets) ? $additional_css_sheets : array();
94b47c30 90 }
91
92
93 /**
94 * Assigns values to template variables
95 *
96 * @param array|string $tpl_var the template variable name(s)
97 * @param mixed $value the value to assign
98 */
99 function assign($tpl_var, $value = NULL) {
100 if (is_array($tpl_var))
101 {
102 foreach ($tpl_var as $key => $val)
103 {
104 if ($key != '')
105 $this->values[$key] = $val;
106 }
107 }
108 else
109 {
110 if ($tpl_var != '')
111 $this->values[$tpl_var] = $value;
112 }
113 }
114
115 /**
116 * Assigns values to template variables by reference
117 *
118 * @param string $tpl_var the template variable name
119 * @param mixed $value the referenced value to assign
120 */
121 function assign_by_ref($tpl_var, &$value)
122 {
123 if ($tpl_var != '')
124 $this->values[$tpl_var] = &$value;
125 }
126
127 /**
128 * Appends values to template variables
129 *
130 * @param array|string $tpl_var the template variable name(s)
131 * @param mixed $value the value to append
132 */
133 function append($tpl_var, $value = NULL, $merge = FALSE)
134 {
135 if (is_array($tpl_var))
136 {
137 foreach ($tpl_var as $_key => $_val)
138 {
139 if ($_key != '')
140 {
141 if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
142 settype($this->values[$_key],'array');
143
144 if($merge && is_array($_val))
145 {
146 foreach($_val as $_mkey => $_mval)
147 $this->values[$_key][$_mkey] = $_mval;
148 }
149 else
150 $this->values[$_key][] = $_val;
151 }
152 }
153 }
154 else
155 {
156 if ($tpl_var != '' && isset($value))
157 {
158 if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
159 settype($this->values[$tpl_var],'array');
160
161 if($merge && is_array($value))
162 {
163 foreach($value as $_mkey => $_mval)
164 $this->values[$tpl_var][$_mkey] = $_mval;
165 }
166 else
167 $this->values[$tpl_var][] = $value;
168 }
169 }
170 }
171
172 /**
173 * Appends values to template variables by reference
174 *
175 * @param string $tpl_var the template variable name
176 * @param mixed $value the referenced value to append
177 */
178 function append_by_ref($tpl_var, &$value, $merge = FALSE)
179 {
180 if ($tpl_var != '' && isset($value))
181 {
182 if(!@is_array($this->values[$tpl_var]))
183 settype($this->values[$tpl_var],'array');
184
185 if ($merge && is_array($value))
186 {
187 foreach($value as $_key => $_val)
188 $this->values[$tpl_var][$_key] = &$value[$_key];
189 }
190 else
191 $this->values[$tpl_var][] = &$value;
192 }
193 }
194
195 /**
196 * Display the template
197 *
198 * @param string $file The template file to use
199 */
200 function display($file)
201 {
44452136 202 // Pull in our config file
94b47c30 203 $t = &$this->values; // place values array directly in scope
44452136 204
205 $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
94b47c30 206 ob_start();
44452136 207 include($template);
94b47c30 208 ob_end_flush();
209 }
210
211 /**
212 * Return the results of applying a template.
213 *
214 * @param string $file The template file to use
215 * @return string A string of the results
216 */
217 function fetch($file)
218 {
219 ob_start();
220 $t = &$this->values; // place values array directly in scope
44452136 221
222 $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
223 include($template);
94b47c30 224 $contents = ob_get_contents();
225 ob_end_clean();
226 return $contents;
227 }
228
44452136 229 /**
230 * Return paths to the required javascript files. Used when generating page
231 * header.
232 *
233 * @return array $paths
234 */
235 function getJavascriptIncludes () {
236 $paths = array();
237 foreach ($this->required_js_files as $file) {
238 if (in_array($file, $this->provided_js_files))
239 $paths[] = './'.$this->template_dir.'js/'.basename($file);
240 else $paths[] = SM_PATH .'templates/default/js/'.basename($file);
241 }
242
243 return $paths;
244 }
3cecf1cd 245
246 /**
247 * Return any additional stylsheets provided by the template. Used when
248 * generating page headers.
249 *
250 * @return array $paths
251 */
252 function getAdditionalStyleSheets () {
253 $paths = array();
254 foreach ($this->additional_css_sheets as $css) {
255 $css = basename($css);
256 if (strtolower($css) == 'stylesheet.tpl') {
257 continue;
258 }
259 $paths[] = $css;
260 }
261 return $paths;
262 }
94b47c30 263}
264
49ea2d20 265?>