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
35 * @note This class was originally created as a part of BIFE.
36 * @author Leandro Lucarella <luca@llucax.hn.org>
37 * @todo Add a global example and an explanation of the directory structure to
40 class MLIB_Tpl_FileDir extends /* implements */ MLIB_Tpl
43 * Root directory where template files are.
48 * If it's true, it looks for template files in PHP's include_path.
50 var $useIncludePath = false;
53 * Group of templates to use.
54 * In this implementation is a subdirectory in $root.
62 * For PHP4 backward compatibility.
64 * @copydoc __construct()
67 function MLIB_Tpl_FileDir($root = '.', $useIncludePath = false, $group = '')
69 $this->__construct($root, $useIncludePath, $group);
75 * @param root Root directory where template files are.
76 * @param useIncludePath If it's true, it looks for template files in
78 * @param group Group of templates to use (a subdirectory in
79 * root in this implementation).
81 * @todo Add an example.
83 function __construct($root = '.', $useIncludePath = false, $group = '')
86 $this->useIncludePath = $useIncludePath;
87 $this->pushGroup($group);
91 * Parse a template returning the results.
92 * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored).
93 * If is a string, {$vars} is replaced with $val.
95 * @copydoc MLIB_Tpl::parse()
96 * @todo Add an example.
98 function parse($name, $vars = '', $val = '', $group = null)
100 $tpl = $this->getFileContent($this->getFilename($name, $group));
102 if (is_string($vars)) {
103 $vars = array($vars => $val);
105 foreach ($vars as $key => $val) {
106 $keys[] = '{' . $key . '}';
109 return str_replace($keys, $vals, $tpl);
116 * Push a new item into the group array.
118 * @param group Item to be added on the group array.
120 * @todo Add an example.
122 function pushGroup($group = '')
124 $this->group[] = $group;
128 * Pops the actual group from the group array.
131 * @todo Add an example.
135 return array_pop($this->group);
139 * Returns the last element of the group array.
142 * @todo Add an example.
146 return end($this->group);
150 * Checks if a certain template file exists.
152 * @param name Template file name.
153 * @param group If it is true, search in the group directory, otherwise
154 * checks only in the root path.
157 * @todo Add an example.
159 function exists($name, $group = null)
161 $tpl = $this->getFileContent($this->getFilename($name, $group));
162 if (!$this->useIncludePath) {
163 return is_readable($file);
165 $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path')));
166 foreach ($include_path as $path) {
167 if (is_readable("$path/$file")) {
176 * Returns the template filename.
177 * Returns the template filename based on the $name and the $group of the
178 * template. If $group is null it uses the current group in the groups
181 * @param name Tempate's name.
182 * @param group Group where to look for the template.
187 function getFilename($name, $group = null)
189 $group = is_null($group) ? end($this->group) : $group;
191 $file = "{$this->root}/$group/$name.tpl.html";
193 $file = "{$this->root}/$name.tpl.html";
199 * Returns the contents of a file as a string.
200 * It uses useIncludePath to search (or not) for the file in the PHP's
203 * @param filename Filename.
206 * @return File contents as a string.
208 function getFileContent($filename)
210 return file_get_contents($filename, $this->useIncludePath);
216 * @example Tpl/FileDir/test.php
218 * This example will create an HTML page with a header, body and a footer.
219 * In order to do this, I will use three template groups (header, body, footer).
220 * All this templates are located in TPLS directory.