Ver Mensaje Individual
  #1 (permalink)  
Antiguo 08/06/2005, 07:42
Spyn_ET
 
Fecha de Ingreso: diciembre-2003
Mensajes: 218
Antigüedad: 20 años, 4 meses
Puntos: 0
Ayuda con Bloques y templates

Buenas chicos/as, resulta q me he puesto a desarrollar una clase, para usar templates propios. Hasta ahora me funciona todo, pero he estado pensando en que quizas necesitaria un tipo {section}{/section} (como Smarty) y no tengo muy claro como plantearme el caso. Osea no se como abordarlo.

La verdad es q no ando muy bien de tiempo y no deberia hacer estas cosas, pero ya sabeis si te picas con algo... :D

Bueno, si teneis tiempo y le echais un ojo os lo agradeceria eternamente.

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                                */        
/*        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
{

    
// -- Variables -- //
        // -- V. de Configuración
    
var $Tini;            //Tag de Inicio.
    
var $Tfin;            //Tag de Fin.
    
var $directorio;    //Directorio del template.

            //Sistema de Cache
    
var $cache false;                //Cache por defecto desactivada.
    
var $cache_dir;
    var 
$cache_time 60;            //Seg.

        // -- V. de Template -- //
    
var $variables = array();        //Variables dentro del template
    
var $fichero;                    //Fichero.
    
    // -- Constructor -- //
    
function SpynTPL($_dir)
    {
        
$this->directorio $_dir;
        
$this->Tini "[";            //Asignamos los tags por defecto.
        
$this->Tfin "]";
    }
    
// -- Cambiamos el directorio de la cache -- //
    
function CmbCacheDir($dir)
    {
        if (
file_exists($dir))
            if (!
is_writable($dir))
                if (!
chmod($dir,0666))
                    die(
"No se puede usar el sistema de cache<br>Compruebe el sistema de directorios");
        
$this->cache_dir $dir;
        
$this->cache true;
    }
    
    
// -- Cambiamos los tags donde meteremos las variables -- //
    
function CmbTags($ini,$fin)
    {
        
$this->Tini $ini;            //Cambiamos los Tags por los que el usuario nos pasa.
        
$this->Tfin $fin;
    }
    
    
// -- Asignamos un valor a las variables -- //
    
function asigna($nombre,$valor)
    {
        
$this->variables[$nombre] = $valor;            //Asignamos valor.
    
}
    
    
// -- Concatena Fichero con fichero anterior -- //
    
function fichero ($fichero)
    {
        if (!
$this->cache)
            {
            if (
file_exists($this->directorio.$fichero))
                
$this->fichero .= $this->TrataFichero(implode("",file($this->directorio.$fichero)));
            }
        else
            
$this->fichero .= $this->FichCache($fichero);
    }
    
    
// -- Comprobamos el fichero de la cache -- //
    
function FichCache($fichero)
    {
        
//Creamos el fichero
        
$creado time() - $this->cache_time;
        echo 
"Ahora = ".time()."<br>Tiempo de cache: ".$this->cache_time." seg.<br>Creado: ".$creado."<br>Creacion del fichero: ";
        if (
file_exists($this->cache_dir.$fichero))
        {
            echo 
filemtime($this->cache_dir.$fichero); 
                if (
filemtime($this->cache_dir.$fichero) < $creado)
                {
                    
$this->cFichCache($fichero);
                    echo 
"<br><b>Creamos el fichero</b>";
                }
            }
        else
            
$this->cFichCache($fichero);
        return 
implode("",file($this->cache_dir.$fichero));
    }
    
    function 
cFichCache ($fichero)
    {
        
$fp fopen($this->cache_dir.$fichero,"w+");
        
fwrite($fp,$this->TrataFichero(implode("",file($this->directorio.$fichero))));
        
fclose($fp); 
    }
    
// -- Muestra el template  -- //
    
function Muestra ()
    {
        
extract($this->variables);
        eval(
"?>".$this->fichero."<?");
    }
    
    
// -- Tratamos el fichero -- //
    
function TrataFichero($fichero)
    {
        
$tmp_file $this->TrataFile($fichero);        
        
//$tmp_file = $this->TrataBloque($tmp_file);
        
$tmp_file str_replace($this->Tini,"<?=",$tmp_file);
        
$tmp_file str_replace($this->Tfin,"?>",$tmp_file);
        return 
$tmp_file;
    }
    
    
// -- Tratamos la opcion {file} -- //
    
function TrataFile ($fichero)
    {
        
// -- Tratamos las inclusiones de ficheros en el template -- //
        
$tmp_file $this->TagFile($fichero);
        while (
eregi($this->Tini."file".$this->Tfin,$tmp_file))
            
$tmp_file $this->TagFile($tmp_file);
        return 
$tmp_file;
    }    

    
// -- Importamos el fichero en caso haber un {file} -- //
    
function TagFile($fichero)
    {
        
$tag 'file';
        
eregi($this->Tini."$tag".$this->Tfin."[a-zA-Z0-9/\.]*".$this->Tini."/$tag".$this->Tfin,$fichero,$array);
        
$ini strlen($this->Tini) + strlen($tag) + strlen($this->Tfin);
        
$fin strlen($this->Tini) + strlen("/$tag") + strlen($this->Tfin);
        
$cuanto strlen($array[0]) - $ini $fin;
        
$file substr($array[0],$ini,$cuanto);
        return 
str_replace($array[0], implode("",file($this->directorio.$file)) ,$fichero);
    }
        
// -- Importamos el fichero en caso haber un {file} -- //
    
function TagBloq($fichero)
    {
        
$tag 'bloq';
        
eregi($this->Tini."$tag".$this->Tfin."[a-zA-Z0-9/\.]*".$this->Tini."/$tag".$this->Tfin,$fichero,$array);
        
$ini strlen($this->Tini) + strlen($tag) + strlen($this->Tfin);
        
$fin strlen($this->Tini) + strlen("/$tag") + strlen($this->Tfin);
        
$cuanto strlen($array[0]) - $ini $fin;
        
$bloq substr($array[0],$ini,$cuanto);
    }
    
}
?>
Por cierto, si creeis q hay algo en el codigo q se puede puede mejorar o cambiar, comentarlo.

Y no hace falta decir q lo podeis usar "si os sirve para algo" (lo dudo :D), sin ningun problema.

Un saludo.
__________________
SymbianForever
SymbianForever.com, todo sobre y para tu symbian
aNieto2K | Themes para WordPress
De todo un poco