From 135039abac08f9546a61b742efd7d921193a2b52 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mart=C3=ADn=20Marrese?= Date: Thu, 29 Jul 2004 18:53:43 +0000 Subject: [PATCH] Se agrega MLIB_Tpl_HIT con pocas modificaciones a lo que tenia hecho Luca. Falta modificar el metodo getFileName porque me parece que funciona mal. --- lib/MLIB/Tpl/HIT.php | 280 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+) create mode 100644 lib/MLIB/Tpl/HIT.php diff --git a/lib/MLIB/Tpl/HIT.php b/lib/MLIB/Tpl/HIT.php new file mode 100644 index 0000000..02e63e4 --- /dev/null +++ b/lib/MLIB/Tpl/HIT.php @@ -0,0 +1,280 @@ + +------------------------------------------------------------------------------- +$Id$ +-----------------------------------------------------------------------------*/ + +/** + * Hooks vs IT Template Engine. + * Hooks vs IT (HIT) is a simple template implementation, based on hooks and IT + * template systems. + * This class was originally created as a part of BIFE. + * Implements HIT + GHIT + BHIT, which includes groups of templates and buffers. + * + * @access public + */ +class MLIB_Tpl_HIT extends MLIB_Tpl +{ + /** + * Root directory where template files are. + * + * @var string $root + * @access public + */ + var $root = '.'; + + /** + * If it's true, it looks for template files in PHP's include_path. + * + * @var bool $useIncludePath + * @access public + */ + var $useIncludePath = false; + + /** + * Group of templates to use (a subdirectory in root). + * + * @var string $group + * @access protected + */ + var $group = ''; + + /** + * Templates cache. + * + * @var array $cache + * @access protected + */ + var $cache = array(); + + /** + * Parsed templates buffer. + * + * @var array $buffer + * @access protected + */ + var $buffer = array(); + + /** + * Constructor. + * + * @param string $root Root directory where template files are. + * @param bool $useIncludePath If it's true, it looks for template files in PHP's include_path. + * @param string $group Group of templates to use (a subdirectory in root). + * + * @return void + * @access public + */ + function HTML_Template_HIT($root = '.', $useIncludePath = false, $group = '') + { + $this->__construct($root, $useIncludePath, $group); + } + + /** + * Constructor. + * + * @param int $root Root directory where template files are. + * @param false $useIncludePath If it's true, it looks for template files in PHP's include_path. + * @param string $group Group of templates to use (a subdirectory in root). + * + * @return void + * @access public + */ + 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. + * + * @param string $name Name of template to parse. + * @param mixed $vars Variables to replace in the template. + * @param string $val If $vars is a string, the value to replace for $vars. + * @param mixed $group Group to use to parse this template. Null to use the current group. + * + * @return string + * @access public + */ + function parse($name, $vars = '', $val = '', $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"; + } + if (!isset($this->cache[$file])) { + $this->cache[$file] = $this->getFileContent($file); + } + if ($vars) { + if (is_string($vars)) { + $vars = array($vars => $val); + } + foreach ($vars as $key => $val) { + $keys[] = '{' . $key . '}'; + $vals[] = $val; + } + return str_replace($keys, $vals, $this->cache[$file]); + } else { + return $this->cache[$file]; + } + } + + /** + * Parse a template adding the results to the buffer. + * Parse a template appending the results to an internal buffer. + * If $vars is an array, the {[keys]} are replaced with [values] ($val is ignored). + * If is a string, {$vars} is replaced with $val. + * + * @param string $name Name of template to parse. + * @param mixed $vars Variables to replace in the template. + * @param string $val If $vars is a string, the value to replace for $vars. + * + * @return void + * @access public + */ + function parseBuffered($name, $vars = '', $val = '') + { + @$this->buffer["{$this->group}/$name"] .= $this->parse($name, $vars, $val); + } + + /** + * Gets a parsed buffer. + * + * @param string $name Name of the parsed template to get. + * + * @return string + * @access public + */ + function getBuffer($name) + { + return @$this->buffer["{$this->group}/$name"]; + } + + /** + * Gets a parsed buffer and removes it. + * + * @param string $name Name of the buffer to flush. + * + * @return void + * @access public + */ + function popBuffer($name) + { + $return = @$this->buffer["{$this->group}/$name"]; + unset($this->buffer["{$this->group}/$name"]); + return $return; + } + + /** + * Sets the group to use and add it to the groups stack. + * + * @param string $group Group to use. + * + * @return void + * @access public + */ + function pushGroup($group = '') + { + $this->group[] = $group; + } + + /** + * Removes the group from the groups stack and returns to the previous used group. + * + * @return string + * @access public + */ + function popGroup() + { + return array_pop($this->group); + } + + /** + * Tells if a template exists. + * True if the template $name exists in $group (or the current group). + * + * @param string $name Name of the template. + * @param mixed $group Template's group. If it's null it uses the current group. + * + * @return bool + * @access public + */ + function exists($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"; + } + 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; + } + } + + + //Agregados debido a la nueva dependencia con MLIB_Tpl' + + /** + * Returns the template file name based on the blockname + * + * @param string $group BlockName. + * + * @access public + * @return mixed + */ + function getFileName($group) + { + //TODO Revisar esto porque asi solo me devuelve el nombre de los + //templates que esten en la barra. + return "{$this->root}/$group.tpl.html"; + } + + /** + * Returns the template file content. + * + * @param string $file Filename. + * + * @access public + * @return mixed + */ + function getFileContent($filename) + { + return file_get_contents($filename, $this->useIncludePath); + } +} +?> -- 2.43.0