1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
4 -------------------------------------------------------------------------------
5 This file is part of mlib.
7 mlib is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your option)
12 mlib is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 You should have received a copy of the GNU Lesser General Public License; if
18 not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 Boston, MA 02111-1307 USA
20 -------------------------------------------------------------------------------
21 Creado: Wed Jun 17 19:03:14 2003
22 Autor: Leandro Lucarella <luca@lugmen.org.ar>
23 -------------------------------------------------------------------------------
25 -----------------------------------------------------------------------------*/
28 * Hooks vs IT Template Engine.
29 * Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT
31 * This class was originally created as a part of BIFE.
32 * Implements HIT + GHIT + BHIT, which includes groups of templates and buffers.
36 class MLIB_Tpl_HIT extends MLIB_Tpl
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();
81 * @param string $root Root directory where template files are.
82 * @param bool $useIncludePath If it's true, it looks for template files in PHP's include_path.
83 * @param string $group Group of templates to use (a subdirectory in root).
88 function HTML_Template_HIT($root = '.', $useIncludePath = false, $group = '')
90 $this->__construct($root, $useIncludePath, $group);
96 * @param int $root Root directory where template files are.
97 * @param false $useIncludePath If it's true, it looks for template files in PHP's include_path.
98 * @param string $group Group of templates to use (a subdirectory in root).
103 function __construct($root = '.', $useIncludePath = false, $group = '')
106 $this->useIncludePath = $useIncludePath;
107 $this->pushGroup($group);
111 * Parse a template returning the results.
112 * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored).
113 * If is a string, {$vars} is replaced with $val.
115 * @param string $name Name of template to parse.
116 * @param mixed $vars Variables to replace in the template.
117 * @param string $val If $vars is a string, the value to replace for $vars.
118 * @param mixed $group Group to use to parse this template. Null to use the current group.
123 function parse($name, $vars = '', $val = '', $group = null)
125 $group = is_null($group) ? end($this->group) : $group;
127 $file = "{$this->root}/$group/$name.tpl.html";
129 $file = "{$this->root}/$name.tpl.html";
131 if (!isset($this->cache[$file])) {
132 $this->cache[$file] = $this->getFileContent($file);
135 if (is_string($vars)) {
136 $vars = array($vars => $val);
138 foreach ($vars as $key => $val) {
139 $keys[] = '{' . $key . '}';
142 return str_replace($keys, $vals, $this->cache[$file]);
144 return $this->cache[$file];
149 * Parse a template adding the results to the buffer.
150 * Parse a template appending the results to an internal buffer.
151 * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored).
152 * If is a string, {$vars} is replaced with $val.
154 * @param string $name Name of template to parse.
155 * @param mixed $vars Variables to replace in the template.
156 * @param string $val If $vars is a string, the value to replace for $vars.
161 function parseBuffered($name, $vars = '', $val = '')
163 @$this->buffer["{$this->group}/$name"] .= $this->parse($name, $vars, $val);
167 * Gets a parsed buffer.
169 * @param string $name Name of the parsed template to get.
174 function getBuffer($name)
176 return @$this->buffer["{$this->group}/$name"];
180 * Gets a parsed buffer and removes it.
182 * @param string $name Name of the buffer to flush.
187 function popBuffer($name)
189 $return = @$this->buffer["{$this->group}/$name"];
190 unset($this->buffer["{$this->group}/$name"]);
195 * Sets the group to use and add it to the groups stack.
197 * @param string $group Group to use.
202 function pushGroup($group = '')
204 $this->group[] = $group;
208 * Removes the group from the groups stack and returns to the previous used group.
215 return array_pop($this->group);
219 * Tells if a template exists.
220 * True if the template $name exists in $group (or the current group).
222 * @param string $name Name of the template.
223 * @param mixed $group Template's group. If it's null it uses the current group.
228 function exists($name, $group = null)
230 $group = is_null($group) ? end($this->group) : $group;
232 $file = "{$this->root}/$group/$name.tpl.html";
234 $file = "{$this->root}/$name.tpl.html";
236 if (!$this->useIncludePath) {
237 return is_readable($file);
239 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
240 foreach ($include_path as $path) {
241 if (is_readable("$path/$file")) {
250 //Agregados debido a la nueva dependencia con MLIB_Tpl'
253 * Returns the template file name based on the blockname
255 * @param string $group BlockName.
260 function getFileName($group)
262 //TODO Revisar esto porque asi solo me devuelve el nombre de los
263 //templates que esten en la barra.
264 return "{$this->root}/$group.tpl.html";
268 * Returns the template file content.
270 * @param string $file Filename.
275 function getFileContent($filename)
277 return file_get_contents($filename, $this->useIncludePath);