Ver Mensaje Individual
  #4 (permalink)  
Antiguo 08/10/2010, 09:39
Hidek1
Colaborador
 
Fecha de Ingreso: octubre-2009
Ubicación: Tokyo - Japan !
Mensajes: 3.867
Antigüedad: 14 años, 6 meses
Puntos: 334
Respuesta: manejo de templates php plano

yo hice una class bastante sencilla para eso

Código PHP:
<?php
class Template
{
    private 
$_filename "";
    public function 
__construct($filename)
    {
        if(
file_exists($filename)) {
            
$this->_filename $filename;
        }else {
            throw new 
Exception("Template no encotrado ( $filename )");
        }
    }
    public function 
render(array $data = array())
    {
        if(empty(
$data)) {
            return 
file_get_contents($this->_filename);
        }else {
            foreach(
$data as $key => $value) $$key $value;
            
$template file_get_contents($this->_filename);
            
$template str_replace(array('\\''\''), array('\\\\''\\\''), $template);
            
$template preg_replace('/\{(\w+?)\}/'"'.$$1.'"$template);
            eval(
"\$template = '$template';");
            return 
$template;
        }
    }
}
para usarla

plantilla.htm
Código HTML:
Ver original
  1. <div>{variable}</div>

Código PHP:
require_once 'Template.php';
$template = new Template('plantilla.htm');
$datos = array(
    
'variable' => 'probando plantilla',
);
echo 
$template->render($datos); 
y el resultado

Código HTML:
Ver original
  1. <div>probando plantilla</div>

saludos.
__________________
More about me...
~ @rhyudek1
~ Github