2 // vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
3 // +--------------------------------------------------------------------+
4 // | BIFE - Buil It FastEr |
5 // +--------------------------------------------------------------------+
6 // | This file is part of BIFE. |
8 // | BIFE is free software; you can redistribute it and/or modify it |
9 // | under the terms of the GNU General Public License as published by |
10 // | the Free Software Foundation; either version 2 of the License, or |
11 // | (at your option) any later version. |
13 // | BIFE is distributed in the hope that it will be useful, but |
14 // | WITHOUT ANY WARRANTY; without even the implied warranty of |
15 // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 // | General Public License for more details. |
18 // | You should have received a copy of the GNU General Public License |
19 // | along with Hooks; if not, write to the Free Software Foundation, |
20 // | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 // +--------------------------------------------------------------------+
22 // | Created: Wed Jun 17 19:03:14 2003 |
23 // | Authors: Leandro Lucarella <luca@lugmen.org.ar> |
24 // +--------------------------------------------------------------------+
29 // +X2C Class 130 :HIT
31 * Hooks vs. IT (HIT) is a simple template implementation, based on hooks and IT template systems.
35 class HTML_Template_HIT {
37 * Root directory where template files are.
45 * If it's true, it looks for template files in PHP's include_path.
47 * @var bool $useIncludePath
50 var $useIncludePath = false;
53 * Group of templates to use (a subdirectory in root).
72 var $buffer = array();
80 * @param string $root Root directory where template files are.
81 * @param bool $useIncludePath If it's true, it looks for template files in PHP's include_path.
82 * @param string $group Group of templates to use (a subdirectory in root).
87 function HTML_Template_HIT($root = '.', $useIncludePath = false, $group = '') // ~X2C
89 $this->__construct($root, $useIncludePath, $group);
97 * @param int $root Root directory where template files are.
98 * @param false $useIncludePath If it's true, it looks for template files in PHP's include_path.
99 * @param string $group Group of templates to use (a subdirectory in root).
104 function __construct($root = '.', $useIncludePath = false, $group = '') // ~X2C
107 $this->useIncludePath = $useIncludePath;
108 $this->pushGroup($group);
112 // +X2C Operation 138
114 * Parse a template returning the results.
115 If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). If is a string, {$vars} is replaced with $val.
117 * @param string $name Name of template to parse.
118 * @param mixed $vars Variables to replace in the template.
119 * @param string $val If $vars is a string, the value to replace for $vars.
120 * @param mixed $group Group to use to parse this template. Null to use the current group.
125 function parse($name, $vars = '', $val = '', $group = null) // ~X2C
127 $group = is_null($group) ? end($this->group) : $group;
129 $file = "{$this->root}/$group/$name.tpl.html";
131 $file = "{$this->root}/$name.tpl.html";
133 if (!isset($this->cache[$file])) {
134 // FIXME - replace join(file()) with file_get_contents().
135 $this->cache[$file] = join('', file($file, $this->useIncludePath));
138 if (is_string($vars)) {
139 $vars = array($vars => $val);
141 foreach ($vars as $key => $val) {
142 $keys[] = '{' . $key . '}';
145 return str_replace($keys, $vals, $this->cache[$file]);
147 return $this->cache[$file];
152 // +X2C Operation 144
154 * Parse a template buffering the results.
155 Parse a template appending the results to an internal buffer. If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). If is a string, {$vars} is replaced with $val.
157 * @param string $name Name of template to parse.
158 * @param mixed $vars Variables to replace in the template.
159 * @param string $val If $vars is a string, the value to replace for $vars.
164 function parseBuffered($name, $vars = '', $val = '') // ~X2C
166 @$this->buffer["{$this->group}/$name"] .= $this->parse($name, $vars, $val);
170 // +X2C Operation 145
172 * Gets a parsed buffer.
174 * @param string $name Name of the parsed template to get.
179 function getBuffer($name) // ~X2C
181 return @$this->buffer["{$this->group}/$name"];
185 // +X2C Operation 146
187 * Gets a parsed buffer and removes it.
189 * @param string $name Name of the buffer to flush.
194 function popBuffer($name) // ~X2C
196 $return = @$this->buffer["{$this->group}/$name"];
197 unset($this->buffer["{$this->group}/$name"]);
202 // +X2C Operation 139
204 * Sets the group to use and add it to the groups stack.
206 * @param string $group Group to use.
211 function pushGroup($group = '') // ~X2C
213 $this->group[] = $group;
217 // +X2C Operation 140
219 * Removes the group from the groups stack and returns to the previous used group.
224 function popGroup() // ~X2C
226 return array_pop($this->group);
230 // +X2C Operation 159
232 * True if the template $name exists in $group (or the current group).
234 * @param string $name Name of the template.
235 * @param mixed $group Template's group. If it's null it uses the current group.
240 function exists($name, $group = null) // ~X2C
242 $group = is_null($group) ? end($this->group) : $group;
244 $file = "{$this->root}/$group/$name.tpl.html";
246 $file = "{$this->root}/$name.tpl.html";
248 if (!$this->useIncludePath) {
249 return is_readable($file);
251 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
252 foreach ($include_path as $path) {
253 if (is_readable("$path/$file")) {