Ver Mensaje Individual
  #2 (permalink)  
Antiguo 12/10/2008, 15:22
edugonch
 
Fecha de Ingreso: agosto-2007
Mensajes: 140
Antigüedad: 16 años, 8 meses
Puntos: 0
Respuesta: ¿Que hariais vosotros?

Muy facil, sigue con php, yo uso templates y mucho css para el diseño, asi me queda independiente del código.

Esta es la clase para la plantilla que uso -->
Código php:
Ver original
  1. class plantilla{
  2.        
  3.     function __construct($template_file)
  4.     {
  5.         $this->tpl_file = $template_file;
  6.     }
  7.        
  8.         function asigna_variables($vars){
  9.                 $this->vars= (empty($this->vars)) ? $vars : $this->vars . $vars;
  10.         }
  11.        
  12.         function retorna()
  13.         {
  14.                 if (!($this->fd = @fopen($this->tpl_file, 'r'))) {
  15.                         echo ('error al abrir la plantilla ' . $this->tpl_file);
  16.                 } else{
  17.                         $this->template_file = fread($this->fd, filesize($this->tpl_file));
  18.                         fclose($this->fd);
  19.                         $this->mihtml = $this->template_file;
  20.                         $this->mihtml = str_replace ("'", "\'", $this->mihtml);
  21.                         $this->mihtml = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml);
  22.                         reset ($this->vars);
  23.                         while (list($key, $val) = each($this->vars)) {
  24.                                 $$key = $val;
  25.                         }
  26.                         eval("\$this->mihtml = '$this->mihtml';");
  27.                         reset ($this->vars);
  28.                         while (list($key, $val) = each($this->vars)) {
  29.                                 unset($$key);
  30.                         }
  31.                         $this->mihtml=str_replace ("\'", "'", $this->mihtml);
  32.                         return $this->mihtml;
  33.                 }
  34.         }
  35.         function muestra()
  36.         {
  37.                     echo $this->retorna();
  38.         }
  39. }

Para usarle pone en la plantilla las áreas a reemplazar como
<!-- Plantilla.html -->
<span class="xxx">{Texto1}</span>
<img src="{DireccionImagen1}" alt="{altImagen}">

Luego en PHP se crea el objeto a partir del archivo html -->

Código php:
Ver original
  1. $PlantillaEjemplo = new Plantilla("Directorio/Plantilla.html");
Y se le asignan las variables -->
Código php:
Ver original
  1. $PlantillaEjemplo->asigna_variables
  2.     'Texto1' => "Hola Mundo",
  3.     'DireccionImagen1' => "/Imagenes/Imagen1.png",
  4.     'altImagen' => "Imagen 1"
  5. ));
Luego se puede almacenar en una variable

Código php:
Ver original
  1. $Variable1 = $PlantillaEjemplo->retorna();

O Imprimir directamente


Código php:
Ver original
  1. $PlantillaEjemplo->muestra();

De esta manera se separa el diseño de la lógica y no tiene que aprender un lenguaje nuevo.

Última edición por edugonch; 13/10/2008 a las 08:16