Ver Mensaje Individual
  #2 (permalink)  
Antiguo 26/11/2009, 06:54
Hidek1
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 15 años, 4 meses
Puntos: 334
Respuesta: Clase templates en PHP

Yo trabajo con templates.. te paso la classe.. funciona perfect :B saludos!


Código php:
Ver original
  1. <?php
  2. class plantilla {
  3.     function plantilla($template_file) {
  4.         $this->html_file = $template_file;
  5.     }
  6.     function asigna_variables($vars) {
  7.         $this->vars = (empty ( $this->vars )) ? $vars : $this->vars . $vars;
  8.     }
  9.     function muestra() {
  10.         if (! ($this->fd = @fopen ( $this->html_file, 'r' ))) {
  11.             sostenedor_error ( 'error al abrir la plantilla ' . $this->html_file );
  12.         } else {
  13.             $this->template_file = fread ( $this->fd, filesize ( $this->html_file ) );
  14.             fclose ( $this->fd );
  15.             $this->mihtml = $this->template_file;
  16.             $this->mihtml = str_replace ( "'", "\'", $this->mihtml );
  17.             $this->mihtml = preg_replace ( '#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml );
  18.             reset ( $this->vars );
  19.             while ( list ( $key, $val ) = each ( $this->vars ) ) {
  20.                 $$key = $val;
  21.             }
  22.             eval ( "\$this->mihtml = '$this->mihtml';" );
  23.             reset ( $this->vars );
  24.             while ( list ( $key, $val ) = each ( $this->vars ) ) {
  25.                 unset ( $$key );
  26.             }
  27.             $this->mihtml = str_replace ( "\'", "'", $this->mihtml );
  28.             echo $this->mihtml;
  29.         }
  30.     }
  31.     function devuelve() {
  32.         if (! ($this->fd = @fopen ( $this->html_file, 'r' ))) {
  33.             self::sostenedor_error ( 'error al abrir la plantilla ' . $this->html_file );
  34.         } else {
  35.             $this->template_file = fread ( $this->fd, filesize ( $this->html_file ) );
  36.             fclose ( $this->fd );
  37.             $this->mihtml = $this->template_file;
  38.             $this->mihtml = str_replace ( "'", "\'", $this->mihtml );
  39.             $this->mihtml = preg_replace ( '#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml );
  40.             reset ( $this->vars );
  41.             while ( list ( $key, $val ) = each ( $this->vars ) ) {
  42.                 $$key = $val;
  43.             }
  44.             eval ( "\$this->mihtml = '$this->mihtml';" );
  45.             reset ( $this->vars );
  46.             while ( list ( $key, $val ) = each ( $this->vars ) ) {
  47.                 unset ( $$key );
  48.             }
  49.             $this->mihtml = str_replace ( "\'", "'", $this->mihtml );
  50.             return $this->mihtml;
  51.         }
  52.     }
  53.    
  54.     function sostenedor_error($error) {
  55.         $miplantilla = new plantilla ( "error.html" );
  56.         $miplantilla->asigna_variables ( array ('ERROR' => $error ) );
  57.         return $miplantilla->muestra ();
  58.     }
  59.     function variable_plantilla($variable) {
  60.         $this->template_file = $variable;
  61.         $this->mihtml = $this->template_file;
  62.         $this->mihtml = str_replace ( "'", "\'", $this->mihtml );
  63.         $this->mihtml = preg_replace ( '#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml );
  64.         reset ( $this->vars );
  65.         while ( list ( $key, $val ) = each ( $this->vars ) ) {
  66.             $$key = $val;
  67.         }
  68.         eval ( "\$this->mihtml = '$this->mihtml';" );
  69.         reset ( $this->vars );
  70.         while ( list ( $key, $val ) = each ( $this->vars ) ) {
  71.             unset ( $$key );
  72.         }
  73.         $this->mihtml = str_replace ( "\'", "'", $this->mihtml );
  74.         return $this->mihtml;
  75.     }
  76. }
  77. ?>
__________________
More about me...
~ @rhyudek1
~ Github