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 Created: Wed Jun 17 19:03:14 2003
22 Authors: Leandro Lucarella <luca@lugmen.org.ar>
23 -------------------------------------------------------------------------------
25 -----------------------------------------------------------------------------*/
27 require_once 'MLIB/Tpl.php';
30 * Hooks vs IT Template Engine.
31 * Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT
34 * @note This class was originally created as a part of BIFE.
35 * @author Leandro Lucarella <luca@llucax.hn.org>
36 * @todo Add a global example and an explanation of the directory structure to
39 class MLIB_Tpl_HIT extends /* implements */ MLIB_Tpl
42 * Root directory where template files are.
47 * If it's true, it looks for template files in PHP's include_path.
49 var $useIncludePath = false;
52 * Group of templates to use.
53 * In this implementation is a subdirectory in $root.
61 * For PHP4 backward compatibility.
63 * @copydoc __construct()
66 function MLIB_Tpl_HIT($root = '.', $useIncludePath = false, $group = '')
68 $this->__construct($root, $useIncludePath, $group);
74 * @param root Root directory where template files are.
75 * @param useIncludePath If it's true, it looks for template files in
77 * @param group Group of templates to use (a subdirectory in
78 * root in this implementation).
80 * @todo Add an example.
82 function __construct($root = '.', $useIncludePath = false, $group = '')
85 $this->useIncludePath = $useIncludePath;
86 $this->pushGroup($group);
90 * Parse a template returning the results.
91 * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored).
92 * If is a string, {$vars} is replaced with $val.
94 * @copydoc MLIB_Tpl::parse()
95 * @todo Add an example.
97 function parse($name, $vars = '', $val = '', $group = null)
99 $tpl = $this->getFileContent($this->getFilename($name, $group));
101 if (is_string($vars)) {
102 $vars = array($vars => $val);
104 foreach ($vars as $key => $val) {
105 $keys[] = '{' . $key . '}';
108 return str_replace($keys, $vals, $tpl);
115 * @copydoc MLIB_Tpl::pushGroup()
117 function pushGroup($group = '')
119 $this->group[] = $group;
123 * @copydoc MLIB_Tpl::popGroup()
127 return array_pop($this->group);
131 * @copydoc MLIB_Tpl::getGroup()
135 return end($this->group);
139 * @copydoc MLIB_Tpl::exists()
141 function exists($name, $group = null)
143 $tpl = $this->getFileContent($this->getFilename($name, $group));
144 if (!$this->useIncludePath) {
145 return is_readable($file);
147 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
148 foreach ($include_path as $path) {
149 if (is_readable("$path/$file")) {
158 * Returns the template filename.
159 * Returns the template filename based on the $name and the $group of the
160 * template. If $group is null it uses the current group in the groups
163 * @param name Tempate's name.
164 * @param group Group where to look for the template.
169 function getFilename($name, $group = null)
171 $group = is_null($group) ? end($this->group) : $group;
173 $file = "{$this->root}/$group/$name.tpl.html";
175 $file = "{$this->root}/$name.tpl.html";
181 * Returns the contents of a file as a string.
182 * It uses useIncludePath to search (or not) for the file in the PHP's
185 * @param filename Filename.
188 * @return File contents as a string.
190 function getFileContent($filename)
192 return file_get_contents($filename, $this->useIncludePath);