text before displayHtmlHeader() causes error notices (#1454409).
[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
45 /**
44452136 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
68 /**
94b47c30 69 * Constructor
70 *
71 * @param string $sTplDir where the template set is located
72 */
49ea2d20 73 function Template($sTplDir) {
94b47c30 74 $this->template_dir = $sTplDir;
44452136 75
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();
81
82# echo 'Template Dir: '.$this->template_dir.': ';
83# var_dump($this->templates_provided);
94b47c30 84 }
85
86
87 /**
88 * Assigns values to template variables
89 *
90 * @param array|string $tpl_var the template variable name(s)
91 * @param mixed $value the value to assign
92 */
93 function assign($tpl_var, $value = NULL) {
94 if (is_array($tpl_var))
95 {
96 foreach ($tpl_var as $key => $val)
97 {
98 if ($key != '')
99 $this->values[$key] = $val;
100 }
101 }
102 else
103 {
104 if ($tpl_var != '')
105 $this->values[$tpl_var] = $value;
106 }
107 }
108
109 /**
110 * Assigns values to template variables by reference
111 *
112 * @param string $tpl_var the template variable name
113 * @param mixed $value the referenced value to assign
114 */
115 function assign_by_ref($tpl_var, &$value)
116 {
117 if ($tpl_var != '')
118 $this->values[$tpl_var] = &$value;
119 }
120
121 /**
122 * Appends values to template variables
123 *
124 * @param array|string $tpl_var the template variable name(s)
125 * @param mixed $value the value to append
126 */
127 function append($tpl_var, $value = NULL, $merge = FALSE)
128 {
129 if (is_array($tpl_var))
130 {
131 foreach ($tpl_var as $_key => $_val)
132 {
133 if ($_key != '')
134 {
135 if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
136 settype($this->values[$_key],'array');
137
138 if($merge && is_array($_val))
139 {
140 foreach($_val as $_mkey => $_mval)
141 $this->values[$_key][$_mkey] = $_mval;
142 }
143 else
144 $this->values[$_key][] = $_val;
145 }
146 }
147 }
148 else
149 {
150 if ($tpl_var != '' && isset($value))
151 {
152 if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
153 settype($this->values[$tpl_var],'array');
154
155 if($merge && is_array($value))
156 {
157 foreach($value as $_mkey => $_mval)
158 $this->values[$tpl_var][$_mkey] = $_mval;
159 }
160 else
161 $this->values[$tpl_var][] = $value;
162 }
163 }
164 }
165
166 /**
167 * Appends values to template variables by reference
168 *
169 * @param string $tpl_var the template variable name
170 * @param mixed $value the referenced value to append
171 */
172 function append_by_ref($tpl_var, &$value, $merge = FALSE)
173 {
174 if ($tpl_var != '' && isset($value))
175 {
176 if(!@is_array($this->values[$tpl_var]))
177 settype($this->values[$tpl_var],'array');
178
179 if ($merge && is_array($value))
180 {
181 foreach($value as $_key => $_val)
182 $this->values[$tpl_var][$_key] = &$value[$_key];
183 }
184 else
185 $this->values[$tpl_var][] = &$value;
186 }
187 }
188
189 /**
190 * Display the template
191 *
192 * @param string $file The template file to use
193 */
194 function display($file)
195 {
44452136 196 // Pull in our config file
94b47c30 197 $t = &$this->values; // place values array directly in scope
44452136 198
199 $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
94b47c30 200 ob_start();
44452136 201 include($template);
94b47c30 202 ob_end_flush();
203 }
204
205 /**
206 * Return the results of applying a template.
207 *
208 * @param string $file The template file to use
209 * @return string A string of the results
210 */
211 function fetch($file)
212 {
213 ob_start();
214 $t = &$this->values; // place values array directly in scope
44452136 215
216 $template = in_array($file, $this->templates_provided) ? $this->template_dir . $file : SM_PATH .'templates/default/'. $file;
217 include($template);
94b47c30 218 $contents = ob_get_contents();
219 ob_end_clean();
220 return $contents;
221 }
222
44452136 223 /**
224 * Return paths to the required javascript files. Used when generating page
225 * header.
226 *
227 * @return array $paths
228 */
229 function getJavascriptIncludes () {
230 $paths = array();
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);
235 }
236
237 return $paths;
238 }
94b47c30 239}
240
49ea2d20 241?>