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 * FileDir is a class based on HIT (Hooks vs IT Template Engine).
31 * Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT
34 * @exapmle MLIB/Tpl/FileDir/test.php
36 * @note This class was originally created as a part of BIFE.
37 * @author Leandro Lucarella <luca@llucax.hn.org>
38 * @todo Add a global example and an explanation of the directory structure to
41 class MLIB_Tpl_FileDir extends /* implements */ MLIB_Tpl
44 * Root directory where template files are.
49 * If it's true, it looks for template files in PHP's include_path.
51 var $useIncludePath = false;
54 * Group of templates to use.
55 * In this implementation is a subdirectory in $root.
63 * For PHP4 backward compatibility.
65 * @copydoc __construct()
68 function MLIB_Tpl_FileDir($root = '.', $useIncludePath = false, $group = '')
70 $this->__construct($root, $useIncludePath, $group);
76 * @param root Root directory where template files are.
77 * @param useIncludePath If it's true, it looks for template files in
79 * @param group Group of templates to use (a subdirectory in
80 * root in this implementation).
82 * @todo Add an example.
84 function __construct($root = '.', $useIncludePath = false, $group = '')
87 $this->useIncludePath = $useIncludePath;
88 $this->pushGroup($group);
92 * Parse a template returning the results.
93 * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored).
94 * If is a string, {$vars} is replaced with $val.
96 * @copydoc MLIB_Tpl::parse()
97 * @todo Add an example.
99 function parse($name, $vars = '', $val = '', $group = null)
101 $tpl = $this->getFileContent($this->getFilename($name, $group));
103 if (is_string($vars)) {
104 $vars = array($vars => $val);
106 foreach ($vars as $key => $val) {
107 $keys[] = '{' . $key . '}';
110 return str_replace($keys, $vals, $tpl);
117 * @copydoc MLIB_Tpl::pushGroup()
119 function pushGroup($group = '')
121 $this->group[] = $group;
125 * @copydoc MLIB_Tpl::popGroup()
129 return array_pop($this->group);
133 * @copydoc MLIB_Tpl::getGroup()
137 return end($this->group);
141 * @copydoc MLIB_Tpl::exists()
143 function exists($name, $group = null)
145 $tpl = $this->getFileContent($this->getFilename($name, $group));
146 if (!$this->useIncludePath) {
147 return is_readable($file);
149 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
150 foreach ($include_path as $path) {
151 if (is_readable("$path/$file")) {
160 * Returns the template filename.
161 * Returns the template filename based on the $name and the $group of the
162 * template. If $group is null it uses the current group in the groups
165 * @param name Tempate's name.
166 * @param group Group where to look for the template.
171 function getFilename($name, $group = null)
173 $group = is_null($group) ? end($this->group) : $group;
175 $file = "{$this->root}/$group/$name.tpl.html";
177 $file = "{$this->root}/$name.tpl.html";
183 * Returns the contents of a file as a string.
184 * It uses useIncludePath to search (or not) for the file in the PHP's
187 * @param filename Filename.
190 * @return File contents as a string.
192 function getFileContent($filename)
194 return file_get_contents($filename, $this->useIncludePath);