Código PHP:
<?
/********************************************************************************/
/* NOMBRE: SpynTPL (Clase) */
/* AUTOR: Andrés Nieto Porras */
/* FECHA: 8/6/05 */
/* DESCRIPCION: */
/* Clase que te permite crear un objeto template para reemplazar */
/* valores de variables insertadas en ficheros HTML. */
/* MODIFICACIONES: */
/* - 8/6/05 Creacion del Fichero */
/* - 9/6/05 Sistema de Debug */
/* - 11/6/05 Permitimos agregar un array directamente. */
/* FUNCIONAMIENTO: */
/* fichero.html */
/* <head> */
/* <title>[$titulo]</title> */
/* </head> */
/* fichero.php */
/* include('class-tpl.php'); */
/* $html = new SpynTPL(); */
/* $html->asigna('titulo','TITULO DE LA PAGINA'); */
/* $html->Muestra(); */
/********************************************************************************/
class SpynTPL
{
var $ini = "["; // -- Tag de comienzo.
var $fin = "]"; // -- Tag de fin.
var $tpl_dir = "."; // -- Directorio de los templates.
var $variables = array(); // -- Variables q componen el template.
var $ficheros = array(); // -- Array de ficheros.
var $pos = -1; // -- Posición dentro de los ficheros.
var $cache = false; // -- Sistema de Cache(true/false).
var $cache_dir = "."; // -- Directorio del cache.
var $cache_time = 10; // -- Tiempo en seg. para q se actualice la pagina.
var $debug = false; // -- Activamos sistema de Debug(true/false).
// -- Constructor -- //
function SpynTPL ($dir, $debug = false)
{
$this->tpl_dir = $dir;
$this->debug = $debug;
}
// -- Cambiamos los tags -- //
function NuevosTags($ini, $fin)
{
$this->ini = $ini;
$this->fin = $fin;
}
// -- Funcion q introduce un array en variables -- //
function AsignaArray($valor)
{
$this->variables[$this->pos] = array_merge($valor);
}
// -- Función q devuelve el fichero de una posicion dada -- //
function DevFichero($posicion)
{
return $this->ficheros[$posicion];
}
// -- Asginamos una variable a un nombre -- //
function Asigna ($nombre = '',$valor)
{
if (is_array($valor))
$this->AsignaArray($valor);
else
{
if ($this->debug)
echo "<br>Asignamos a $this->ficheros[$this->pos]: $nombre => $valor";
$this->variables[$this->pos][$nombre]= $valor;
}
}
/* -- Sistema de Cache -- */
// -- Activamos la cache -- //
function ActivaCache()
{
if ($this->debug)
echo "<br><b>Activamos CACHE</b><br>";
$this->cache = true;
}
// -- Cambiamos el tiempo de la cache (seg) -- //
function TiempoCache($tiempo)
{
$this->cache_time = $tiempo;
}
// -- Cambiamos el directorio de la cache -- //
function DirectorioCache ($dir)
{
$this->cache_dir = $dir;
}
/* -- Fin Sistema de Cache -- */
// -- Insertamos un nuevo fichero en el array -- //
function Fichero ($file)
{
if ($this->cache) // Si tenemos la cache activada
$this->ficheros[$this->pos++] = $this->ExisteEnCache($file); // Si no existe el fichero en cache
else
if (file_exists($this->tpl_dir.$file)) //Si existe el fichero.
$this->ficheros[$this->pos++] = $file;
else
die("No existe el fichero $this->tpl_dir.$file");
}
// -- Comprobamos que exista el fichero en la cache -- //
function ExisteEnCache($file)
{
//Creamos el fichero
$creado = time() - $this->cache_time;
if ($this->debug)
echo "<b>Ahora</b> = ".time()."<br><b>Tiempo de cache</b>: ".$this->cache_time." seg.<br><b>Tiempo valido antes de borrar:</b> ".$creado.
"<br><b>Creacion del fichero:</b> ";
if (file_exists($this->cache_dir.$file)) // -- Si el fichero existe
{
if ($this->debug)
echo filemtime($this->cache_dir.$file)."<br>";
if (filemtime($this->cache_dir.$file) < $creado) // -- Comprobamos q no exceda el tiempo maximo
{
if ($this->debug)
echo "Entramos ".filemtime($this->cache_dir.$file)." < $creado";
$salida = $this->NuevoEnCache($file); // -- Lo creamos
}
$salida = $file;
}
else
$salida = $this->NuevoEnCache($file); // -- Si no existe, lo creamos
if ($this->debug)
echo "FICHERO EN CACHE: ".$salida;
return $salida;
}
// -- Creamos fichero nuevo en cache -- //
function NuevoEnCache ($file)
{
$nuevoNombre = $file;
$fp = fopen($this->cache_dir.$nuevoNombre,"w");
fwrite($fp,$this->TrataFichero($this->tpl_dir.$file));
fclose($fp);
if ($this->debug)
echo "<br><b>Creamos el fichero</b><br><hr>";
return $nuevoNombre;
}
// -- Recorremos los ficheros del array -- //
function Muestra()
{
$x = 0;
if ($this->debug)
echo "<br><b>Total ficheros:</b> ". $this->pos;
foreach($this->ficheros as $actual)
{
if ($this->debug)
echo "<br>Fichero actual: $actual";
$this->mostrarTemplate($actual,$x);
$x++;
}
}
// -- Funcion que muestra un template -- //
function mostrarTemplate($file,$pos)
{
if ($this->debug)
echo "<br>Posicion: $pos";
if (!$this->cache)
$fichSalida = $this->TrataFichero($this->tpl_dir.$file);
else
$fichSalida = implode("", file($this->cache_dir.$file));
if (isset($this->variables[$pos]))
if (is_array($this->variables[$pos]))
extract($this->variables[$pos]);
eval("?>".$fichSalida."<?");
}
// -- Tratamos el fichero -- //
function TrataFichero($fichero)//----------------------------------------------------------------TRATAFICHERO
{
$impFichero = implode ("",file($fichero));
$impFichero = str_replace($this->ini,"<?=",$impFichero);
$impFichero = str_replace($this->fin,"?>",$impFichero);
if ($this->debug)
highlight_string($impFichero);
return $impFichero;
}
}
Un saludo.