Código PHP:
<?
class plantilla{
function plantilla($template_file)
{
$this->tpl_file = 'plantillas/' . $template_file . '.tpl';
}
function asigna_variables($vars)
{
$this->vars= (empty($this->vars)) ? $vars : $this->vars . $vars;
}
function muestra(){
if (!($this->fd = @fopen($this->tpl_file, 'r')))
{
sostenedor_error('error al abrir la plantilla ' . $this->tpl_file);
}else{
$this->template_file = fread($this->fd, filesize($this->tpl_file));
fclose($this->fd);
$this->mihtml = $this->template_file;
$this->mihtml = str_replace ("'", "\'", $this->mihtml);
$this->mihtml = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->mihtml);
reset ($this->vars);
while (list($key, $val) = each($this->vars))
{
$$key = $val;
}
eval("\$this->mihtml = '$this->mihtml';");
reset ($this->vars);
while (list($key, $val) = each($this->vars))
{
unset($$key);
}
$this->mihtml=str_replace ("\'", "'", $this->mihtml);
echo $this->mihtml;
///
}
}
}
function sostenedor_error($error){
$miplantilla=new plantilla("error");
$miplantilla->asigna_variables(array(
'ERROR' => $error)
);
return $miplantilla->muestra();
}
?>
Código PHP:
<?php
class Template{
var $tpl_file, $htmlTemplate;
var $htmlText;
var $fileReaded;
var $PATH = 'Templates/';
var $EXT = '.tpl';
function Template(){
}
function setTemplate( $templateFile ){
$this->htmlText="";
$this->tpl_file = $this->PATH . $templateFile . $this->EXT ;
$this->fileReaded = $this->fileData = @fopen($this->tpl_file, 'r');
if (!$this->fileReaded) {
return false;
} else{
$this->htmlTemplate = fread($this->fileData, filesize($this->tpl_file));
$this->htmlTemplate = str_replace ("'", "\'", $this->htmlTemplate);
fclose($this->fileData);
}
return true;
}
function setVars($vars){
if ( $this->fileReaded ) {
$this->vars = $vars;
$this->htmlText = preg_replace('#\{([a-z0-9\-_]*?)\}#is', "' . $\\1 . '", $this->htmlTemplate);
reset ($this->vars);
while (list($key, $val) = each($this->vars)) {
$$key = $val;
}
eval("\$this->htmlText = '$this->htmlText';");
reset ($this->vars);
while (list($key, $val) = each($this->vars)) {
unset($$key);
}
$this->htmlText = str_replace ("\'", "'", $this->htmlText);
return true;
}else{
//Error, you must set a template file
return false;
}
}
function show(){
if ( $this->fileReaded ) {
return ($this->htmlText!="")?$this->htmlText:$this->htmlTemplate;
}else{
//Error, you must set a template file
return "[ERROR]";
}
}
}
?>
y en la pagina index.php
pongo esto:
Código PHP:
<?
include("clase_plantilla.php");
$Contenido=new Plantilla("holaMundo");//al Pasar como parametro holaMundo, asumimos que en la carpeta plantillas existe un archivo de nombre holaMundo.tpl
$Contenido->asigna_variables(array( "variable" => "Hola Mundo2"));
$ContenidoString = $Contenido->muestra();//$ContenidoString contiene nuestra plantilla, ya con las variables asignadas
echo $ContenidoString."<br>";
?>
Simplemente podrian darme un pequeño ejemplo
Salu2.