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 * Group of templates to use (a subdirectory in root).
64 var $buffer = array();
72 * @param string $root Root directory where template files are.
73 * @param string $group Group of templates to use (a subdirectory in root).
78 function HTML_Template_HIT($root = '.', $group = '') // ~X2C
80 $this->__construct($root, $group);
88 * @param int $root Root directory where template files are.
89 * @param int $group Group of templates to use (a subdirectory in root).
94 function __construct($root = '.', $group = '') // ~X2C
97 $this->setGroup($group);
101 // +X2C Operation 138
103 * Parse a template returning the results.
104 If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). If is a string, {$vars} is replaced with $val.
106 * @param string $name Name of template to parse.
107 * @param mixed $vars Variables to replace in the template.
108 * @param string $val If $vars is a string, the value to replace for $vars.
113 function parse($name, $vars = '', $val = '') // ~X2C
115 $group = end($this->group);
117 $file = "{$this->root}/$group/$name.tpl.html";
119 $file = "{$this->root}/$name.tpl.html";
121 if (!isset($this->cache[$file])) {
122 // FIXME - replace join(file()) with file_get_contents().
123 $this->cache[$file] = join('', file($file));
125 //if (!is_readable($file)) {
126 // trigger_error("Can't read '$file'.");
129 if (is_string($vars)) {
130 $vars = array($vars => $val);
132 foreach ($vars as $key => $val) {
133 $keys[] = '{' . $key . '}';
136 return str_replace($keys, $vals, $this->cache[$file]);
138 return $this->cache[$file];
143 // +X2C Operation 144
145 * Parse a template buffering the results.
146 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.
148 * @param string $name Name of template to parse.
149 * @param mixed $vars Variables to replace in the template.
150 * @param string $val If $vars is a string, the value to replace for $vars.
155 function parseBuffered($name, $vars = '', $val = '') // ~X2C
157 @$this->buffer["{$this->group}/$name"] .= $this->parse($name, $vars, $val);
161 // +X2C Operation 145
163 * Gets a parsed buffer.
165 * @param string $name Name of the parsed template to get.
166 * @param bool $flush Flush buffer.
171 function getBuffer($name, $flush = true) // ~X2C
173 return @$this->buffer["{$this->group}/$name"];
177 // +X2C Operation 146
179 * Gets a parsed buffer and removes it.
181 * @param int $name Name of the buffer to flush.
186 function popBuffer($name = '') // ~X2C
188 $return = @$this->buffer["{$this->group}/$name"];
189 unset($this->buffer["{$this->group}/$name"]);
194 // +X2C Operation 139
196 * Sets the group to use and add it to the groups stack.
198 * @param string $group Group to use.
203 function setGroup($group = '') // ~X2C
206 $this->group[] = $group;
213 // +X2C Operation 140
215 * Removes the group from the groups stack and returns to the previous used group.
220 function unsetGroup() // ~X2C
222 array_pop($this->group);