Templates
[squirrelmail.git] / class / template / template.class.php
CommitLineData
94b47c30 1<?php
2/**
3 * Copyright 2003, Paul James
4 * Copyright (c) 2005 The SquirrelMail Project Team
5 * Licensed under the GNU GPL. For full terms see the file COPYING.
6 *
7 * This file contains some methods from the Smarty templating engine version
8 * 2.5.0 by Monte Ohrt <monte@ispi.net> and Andrei Zmievski <andrei@php.net>.
9 *
10 * @version $Id$
11 */
12
13/**
14 * The SquirrelMail (Foowd) template implementation.
15 * Derived from the foowd template implementation and adapted
16 * for squirrelmail
17 * @package SquirrelMail
18 */
19
20/**
21 * The SquirrelMail (Foowd) template class.
22 *
23 * Basic template class for capturing values and pluging them into a template.
24 * This class uses a similar API to Smarty.
25 *
26 * @author Paul James
27 * @author Monte Ohrt <monte@ispi.net>
28 * @author Andrei Zmievski <andrei@php.net>
29 * @package SquirrelMail
30 */
31class Template
32{
33 /**
34 * The templates values array
35 *
36 * @var array
37 */
38 var $values = array();
39
40 /**
41 * The template directory to use
42 *
43 * @var string
44 */
45 var $template_dir = 'templates\default';
46
47 /**
48 * Constructor
49 *
50 * @param string $sTplDir where the template set is located
51 */
52 function Template($sTplDir = 'templates\default') {
53 $this->template_dir = $sTplDir;
54 }
55
56
57 /**
58 * Assigns values to template variables
59 *
60 * @param array|string $tpl_var the template variable name(s)
61 * @param mixed $value the value to assign
62 */
63 function assign($tpl_var, $value = NULL) {
64 if (is_array($tpl_var))
65 {
66 foreach ($tpl_var as $key => $val)
67 {
68 if ($key != '')
69 $this->values[$key] = $val;
70 }
71 }
72 else
73 {
74 if ($tpl_var != '')
75 $this->values[$tpl_var] = $value;
76 }
77 }
78
79 /**
80 * Assigns values to template variables by reference
81 *
82 * @param string $tpl_var the template variable name
83 * @param mixed $value the referenced value to assign
84 */
85 function assign_by_ref($tpl_var, &$value)
86 {
87 if ($tpl_var != '')
88 $this->values[$tpl_var] = &$value;
89 }
90
91 /**
92 * Appends values to template variables
93 *
94 * @param array|string $tpl_var the template variable name(s)
95 * @param mixed $value the value to append
96 */
97 function append($tpl_var, $value = NULL, $merge = FALSE)
98 {
99 if (is_array($tpl_var))
100 {
101 foreach ($tpl_var as $_key => $_val)
102 {
103 if ($_key != '')
104 {
105 if(isset($this->values[$_key]) && !is_array($this->values[$_key]))
106 settype($this->values[$_key],'array');
107
108 if($merge && is_array($_val))
109 {
110 foreach($_val as $_mkey => $_mval)
111 $this->values[$_key][$_mkey] = $_mval;
112 }
113 else
114 $this->values[$_key][] = $_val;
115 }
116 }
117 }
118 else
119 {
120 if ($tpl_var != '' && isset($value))
121 {
122 if(isset($this->values[$tpl_var]) && !is_array($this->values[$tpl_var]))
123 settype($this->values[$tpl_var],'array');
124
125 if($merge && is_array($value))
126 {
127 foreach($value as $_mkey => $_mval)
128 $this->values[$tpl_var][$_mkey] = $_mval;
129 }
130 else
131 $this->values[$tpl_var][] = $value;
132 }
133 }
134 }
135
136 /**
137 * Appends values to template variables by reference
138 *
139 * @param string $tpl_var the template variable name
140 * @param mixed $value the referenced value to append
141 */
142 function append_by_ref($tpl_var, &$value, $merge = FALSE)
143 {
144 if ($tpl_var != '' && isset($value))
145 {
146 if(!@is_array($this->values[$tpl_var]))
147 settype($this->values[$tpl_var],'array');
148
149 if ($merge && is_array($value))
150 {
151 foreach($value as $_key => $_val)
152 $this->values[$tpl_var][$_key] = &$value[$_key];
153 }
154 else
155 $this->values[$tpl_var][] = &$value;
156 }
157 }
158
159 /**
160 * Display the template
161 *
162 * @param string $file The template file to use
163 */
164 function display($file)
165 {
166 $t = &$this->values; // place values array directly in scope
167 ob_start();
168 include($this->template_dir.$file);
169 ob_end_flush();
170 }
171
172 /**
173 * Return the results of applying a template.
174 *
175 * @param string $file The template file to use
176 * @return string A string of the results
177 */
178 function fetch($file)
179 {
180 ob_start();
181 $t = &$this->values; // place values array directly in scope
182 include($this->template_dir.$file);
183 $contents = ob_get_contents();
184 ob_end_clean();
185 return $contents;
186 }
187
188}
189
190?>