Ver Mensaje Individual
  #1 (permalink)  
Antiguo 05/02/2012, 17:35
tomirammstein
 
Fecha de Ingreso: septiembre-2011
Mensajes: 52
Antigüedad: 12 años, 7 meses
Puntos: 0
Exclamación [AYUDA]clase de templates no pasa arrays.

Hola a todos, tengo un problema al pasar un array por la clase de templates.

Esta es la clase:

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

Entonces, cuando asigno las variables a pasar al documento html, sería algo así :

Código PHP:
Ver original
  1. require('_clases/clase_plantilla.php');
  2. $profileTemplate=new Plantilla('profile');
  3. $profileTemplate->asigna_variables(array(
  4.                 "titulo" => "algo"
  5.             ));
  6. $ContenidoString = $profileTemplate->muestra();
  7. echo $ContenidoString;

Entonces, en el documento tpl:

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <title>{titulo}</title>
  3. </head>
  4. </body>
  5. </html>

Lo de arriba imrpimiría "algo", ya que es el valor que le di a la variable

Pero, ¿que tal si quiero pasar arrays?, tal vez consultas de sql, como por ejemplo:

Código PHP:
Ver original
  1. mysql_connect('localhost', 'root', '');
  2. $_q=mysql_query("SELECT * FROM tabla WHERE id_usuario = 1");
  3. while($_query = mysql_fetch_array($_q)){
  4.   $_queryUser = $_query['user'];
  5. }
  6. require('_clases/clase_plantilla.php');
  7. $profileTemplate=new Plantilla('profile');
  8. $profileTemplate->asigna_variables(array(
  9.                 "nombres" => $_queryUser
  10.             ));
  11. $ContenidoString = $profileTemplate->muestra();
  12. echo $ContenidoString;

Pero en el documento TPL solo me sale un solo resultado, entonces necesito no solo pasar variables, sino ARRAYS, pero no sé como hacerlo.
¿Alguien me da una mano?
Gracias desde ya.