7bc8ad62dd7ec097da254d025fea39a6581aad2e
[squirrelmail.git] / class / template / PHP_Template.class.php
1 <?php
2
3 /**
4 * Copyright 2003, Paul James
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 *
9 * The SquirrelMail (Foowd) template implementation.
10 * Derived from the foowd template implementation and adapted
11 * for squirrelmail
12 * @copyright &copy; 2005-2006 The SquirrelMail Project Team
13 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @version $Id$
15 * @package squirrelmail
16 *
17 */
18
19 /**
20 * The SquirrelMail PHP Template class. Extends the base
21 * Template class for use with PHP template pages.
22 *
23 * @author Paul James
24 * @author Monte Ohrt <monte at ispi.net>
25 * @author Andrei Zmievski <andrei at php.net>
26 * @author Paul Lesniewski
27 * @package squirrelmail
28 *
29 */
30 class PHP_Template extends Template
31 {
32
33 /**
34 * The templates values array
35 *
36 * @var array
37 *
38 */
39 var $values = array();
40
41
42 /**
43 * Constructor
44 *
45 * Please do not call directly. Use Template::construct_template().
46 *
47 * @param string $template_id the template ID
48 *
49 */
50 function PHP_Template($template_id) {
51 //FIXME: find a way to test that this is ONLY ever called
52 // from parent's construct_template() method (I doubt it
53 // is worth the trouble to parse the current stack trace)
54 // if (???)
55 // trigger_error('Please do not use default PHP_Template() constructor. Instead, use Template::construct_template().', E_USER_ERROR);
56
57 parent::Template($template_id);
58
59 }
60
61 /**
62 * Assigns values to template variables
63 *
64 * @param array|string $tpl_var the template variable name(s)
65 * @param mixed $value the value to assign
66 FIXME: Proposed idea to add a parameter here that turns variable
67 encoding on, so that we can make sure output is always
68 run through something like htmlspecialchars() (maybe even nl2br()?)
69 *
70 */
71 function assign($tpl_var, $value = NULL) {
72
73 if (is_array($tpl_var))
74 {
75 foreach ($tpl_var as $key => $val)
76 {
77 if ($key != '')
78 $this->values[$key] = $val;
79 }
80 }
81 else
82 {
83 if ($tpl_var != '')
84 $this->values[$tpl_var] = $value;
85 }
86
87 }
88
89 /**
90 * Assigns values to template variables by reference
91 *
92 * @param string $tpl_var the template variable name
93 * @param mixed $value the referenced value to assign
94 FIXME: Proposed idea to add a parameter here that turns variable
95 encoding on, so that we can make sure output is always
96 run through something like htmlspecialchars() (maybe even nl2br()?)
97 *
98 */
99 function assign_by_ref($tpl_var, &$value) {
100
101 if ($tpl_var != '')
102 $this->values[$tpl_var] = &$value;
103
104 }
105
106 /**
107 * Appends values to template variables
108 *
109 * @param array|string $tpl_var the template variable name(s)
110 * @param mixed $value the value to append
111 * @param boolean $merge when $value is given as an array,
112 * this indicates whether or not that
113 * array itself should be appended as
114 * a new template variable value or if
115 * that array's values should be merged
116 * into the existing array of template
117 * variable values
118 FIXME: Proposed idea to add a parameter here that turns variable
119 encoding on, so that we can make sure output is always
120 run through something like htmlspecialchars() (maybe even nl2br()?)
121 *
122 */
123 function append($tpl_var, $value = NULL, $merge = FALSE)
124 {
125 if (is_array($tpl_var))
126 {
127 foreach ($tpl_var as $_key => $_val)
128 {
129 if ($_key != '')
130 {
131 if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
132 settype($this->values[$_key],'array');
133
134 if($merge && is_array($_val))
135 {
136 // FIXME: Tentative testing seems to indicate that
137 // this does not mirror Smarty behavior; Smarty
138 // seems to append the full array as a new element
139 // instead of merging, so this behavior is technically
140 // more "correct", but Smarty seems to differ
141 foreach($_val as $_mkey => $_mval)
142 $this->values[$_key][$_mkey] = $_mval;
143 }
144 else
145 $this->values[$_key][] = $_val;
146 }
147 }
148 }
149 else
150 {
151 if ($tpl_var != '' && isset($value))
152 {
153 if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
154 settype($this->values[$tpl_var],'array');
155
156 if($merge && is_array($value))
157 {
158 foreach($value as $_mkey => $_mval)
159 $this->values[$tpl_var][$_mkey] = $_mval;
160 }
161 else
162 $this->values[$tpl_var][] = $value;
163 }
164 }
165 }
166
167 /**
168 * Appends values to template variables by reference
169 *
170 * @param string $tpl_var the template variable name
171 * @param mixed $value the referenced value to append
172 * @param boolean $merge when $value is given as an array,
173 * this indicates whether or not that
174 * array itself should be appended as
175 * a new template variable value or if
176 * that array's values should be merged
177 * into the existing array of template
178 * variable values
179 FIXME: Proposed idea to add a parameter here that turns variable
180 encoding on, so that we can make sure output is always
181 run through something like htmlspecialchars() (maybe even nl2br()?)
182 *
183 */
184 function append_by_ref($tpl_var, &$value, $merge = FALSE)
185 {
186 if ($tpl_var != '' && isset($value))
187 {
188 if(!@is_array($this->values[$tpl_var]))
189 settype($this->values[$tpl_var],'array');
190
191 if ($merge && is_array($value))
192 {
193 foreach($value as $_key => $_val)
194 $this->values[$tpl_var][$_key] = &$value[$_key];
195 }
196 else
197 $this->values[$tpl_var][] = &$value;
198 }
199 }
200
201 /**
202 * Applys the template and generates final output destined
203 * for the user's browser
204 *
205 * @param string $filepath The full file path to the template to be applied
206 *
207 * @return string The output for the given template
208 *
209 */
210 function apply_template($filepath) {
211
212 // place values array directly in scope
213 // ($t? let's try to be more verbose please :-) )
214 //
215 $t = &$this->values;
216
217 ob_start();
218 include($filepath);
219 $contents = ob_get_contents();
220 ob_end_clean();
221 return $contents;
222
223 }
224
225 }
226