------------------------------------------------------------------------------- $Id$ -----------------------------------------------------------------------------*/ require_once 'MLIB/Tpl.php'; /** * Hooks vs IT Template Engine. * Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT * template systems. * * @note This class was originally created as a part of BIFE. * @author Leandro Lucarella * @todo Add a global example and an explanation of the directory structure to * use, etc. */ class MLIB_Tpl_HIT extends /* implements */ MLIB_Tpl { /** * Root directory where template files are. */ var $root = '.'; /** * If it's true, it looks for template files in PHP's include_path. */ var $useIncludePath = false; /** * Group of templates to use. * In this implementation is a subdirectory in $root. * * @protected */ var $group = ''; /** * Constructor. * For PHP4 backward compatibility. * * @copydoc __construct() * @see __construct() */ function MLIB_Tpl_HIT($root = '.', $useIncludePath = false, $group = '') { $this->__construct($root, $useIncludePath, $group); } /** * Constructor. * * @param root Root directory where template files are. * @param useIncludePath If it's true, it looks for template files in * PHP's include_path. * @param group Group of templates to use (a subdirectory in * root in this implementation). * * @todo Add an example. */ function __construct($root = '.', $useIncludePath = false, $group = '') { $this->root = $root; $this->useIncludePath = $useIncludePath; $this->pushGroup($group); } /** * Parse a template returning the results. * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). * If is a string, {$vars} is replaced with $val. * * @copydoc MLIB_Tpl::parse() * @todo Add an example. */ function parse($name, $vars = '', $val = '', $group = null) { $tpl = $this->getFileContent($this->getFilename($name, $group)); if ($vars) { if (is_string($vars)) { $vars = array($vars => $val); } foreach ($vars as $key => $val) { $keys[] = '{' . $key . '}'; $vals[] = $val; } return str_replace($keys, $vals, $tpl); } else { return $tpl; } } /** * @copydoc MLIB_Tpl::pushGroup() */ function pushGroup($group = '') { $this->group[] = $group; } /** * @copydoc MLIB_Tpl::popGroup() */ function popGroup() { return array_pop($this->group); } /** * @copydoc MLIB_Tpl::getGroup() */ function getGroup() { return end($this->group); } /** * @copydoc MLIB_Tpl::exists() */ function exists($name, $group = null) { $tpl = $this->getFileContent($this->getFilename($name, $group)); if (!$this->useIncludePath) { return is_readable($file); } else { $include_path = array_unique(preg_split('/[:;]/', ini_get('include_path'))); foreach ($include_path as $path) { if (is_readable("$path/$file")) { return true; } } return false; } } /** * Returns the template filename. * Returns the template filename based on the $name and the $group of the * template. If $group is null it uses the current group in the groups * stack. * * @param name Tempate's name. * @param group Group where to look for the template. * * @protected * @return Filename. */ function getFilename($name, $group = null) { $group = is_null($group) ? end($this->group) : $group; if ($group) { $file = "{$this->root}/$group/$name.tpl.html"; } else { $file = "{$this->root}/$name.tpl.html"; } return $file; } /** * Returns the contents of a file as a string. * It uses useIncludePath to search (or not) for the file in the PHP's * include_path. * * @param filename Filename. * * @protected * @return File contents as a string. */ function getFileContent($filename) { return file_get_contents($filename, $this->useIncludePath); } } ?>