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 Template Engine.
32 Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT template systems.
34 * @package HTML_Template
37 class HTML_Template_HIT {
39 * Root directory where template files are.
47 * If it's true, it looks for template files in PHP's include_path.
49 * @var bool $useIncludePath
52 var $useIncludePath = false;
55 * Group of templates to use (a subdirectory in root).
71 * Parsed templates buffer.
76 var $buffer = array();
84 * @param string $root Root directory where template files are.
85 * @param bool $useIncludePath If it's true, it looks for template files in PHP's include_path.
86 * @param string $group Group of templates to use (a subdirectory in root).
91 function HTML_Template_HIT($root = '.', $useIncludePath = false, $group = '') // ~X2C
93 $this->__construct($root, $useIncludePath, $group);
101 * @param int $root Root directory where template files are.
102 * @param false $useIncludePath If it's true, it looks for template files in PHP's include_path.
103 * @param string $group Group of templates to use (a subdirectory in root).
108 function __construct($root = '.', $useIncludePath = false, $group = '') // ~X2C
111 $this->useIncludePath = $useIncludePath;
112 $this->pushGroup($group);
116 // +X2C Operation 138
118 * Parse a template returning the results.
119 If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). If is a string, {$vars} is replaced with $val.
121 * @param string $name Name of template to parse.
122 * @param mixed $vars Variables to replace in the template.
123 * @param string $val If $vars is a string, the value to replace for $vars.
124 * @param mixed $group Group to use to parse this template. Null to use the current group.
129 function parse($name, $vars = '', $val = '', $group = null) // ~X2C
131 $group = is_null($group) ? end($this->group) : $group;
133 $file = "{$this->root}/$group/$name.tpl.html";
135 $file = "{$this->root}/$name.tpl.html";
137 if (!isset($this->cache[$file])) {
138 // FIXME - replace join(file()) with file_get_contents().
139 $this->cache[$file] = join('', file($file, $this->useIncludePath));
142 if (is_string($vars)) {
143 $vars = array($vars => $val);
145 foreach ($vars as $key => $val) {
146 $keys[] = '{' . $key . '}';
149 return str_replace($keys, $vals, $this->cache[$file]);
151 return $this->cache[$file];
156 // +X2C Operation 144
158 * Parse a template adding the results to the buffer.
159 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.
161 * @param string $name Name of template to parse.
162 * @param mixed $vars Variables to replace in the template.
163 * @param string $val If $vars is a string, the value to replace for $vars.
168 function parseBuffered($name, $vars = '', $val = '') // ~X2C
170 @$this->buffer["{$this->group}/$name"] .= $this->parse($name, $vars, $val);
174 // +X2C Operation 145
176 * Gets a parsed buffer.
178 * @param string $name Name of the parsed template to get.
183 function getBuffer($name) // ~X2C
185 return @$this->buffer["{$this->group}/$name"];
189 // +X2C Operation 146
191 * Gets a parsed buffer and removes it.
193 * @param string $name Name of the buffer to flush.
198 function popBuffer($name) // ~X2C
200 $return = @$this->buffer["{$this->group}/$name"];
201 unset($this->buffer["{$this->group}/$name"]);
206 // +X2C Operation 139
208 * Sets the group to use and add it to the groups stack.
210 * @param string $group Group to use.
215 function pushGroup($group = '') // ~X2C
217 $this->group[] = $group;
221 // +X2C Operation 140
223 * Removes the group from the groups stack and returns to the previous used group.
228 function popGroup() // ~X2C
230 return array_pop($this->group);
234 // +X2C Operation 159
236 * Tells if a template exists.
237 True if the template $name exists in $group (or the current group).
239 * @param string $name Name of the template.
240 * @param mixed $group Template's group. If it's null it uses the current group.
245 function exists($name, $group = null) // ~X2C
247 $group = is_null($group) ? end($this->group) : $group;
249 $file = "{$this->root}/$group/$name.tpl.html";
251 $file = "{$this->root}/$name.tpl.html";
253 if (!$this->useIncludePath) {
254 return is_readable($file);
256 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
257 foreach ($include_path as $path) {
258 if (is_readable("$path/$file")) {