bueno tu solucion es optima pero, esa clase que te pase era una antigua y de poca funcionalidad , ahora no puedo publciar el codigo de desarrollo de la nueva clase por que es parte de un proyecto que se esta gestando por lo que te muestro , lo que se los otros desarrolladores ven , un archivo solo te muestra los comentarios y funciones que puedes usar de la plantilla, y el otro archivo es el codigo OFUSCADO y minimizado osea el funcional,
comentarios de la clase myTemaplateCódigo PHP :
class myTemplate
{
private $vars = array();
private $assigns = array();
private $PATH;
private $URL = '.';
private $URLS = array();
private $PATHS = array();
private $_extension = '.tpl.php';
private $_level = 0;
private $_levels = array();
private $_template_eval = array();
private $_last_error = '';
private $_is_multipath = false;
const DEBUG = MODE_DEBUG;
static public function newInstance($path ,$extension = false){}
/**
* @param string $path ubicacion de la carpeta de plantillas
*/
public function __construct($path ,$extension = false){}
public function setUrl($url){}
/**
* @param array $array este array solo debe ser usado en arrays de asignacion con valor cadena,numerico,booleano de tipo array(clave => valor )
* @return myTemplate
*/
public function vars($array){}
/**
* @param string $var solo acepta cadenas
* @param string|int|boolean $value el valor de la variable
* @return plantilla_tuadmin
*/
public function _($var,$value){}
/**
* @param array $array este array solo debe ser usado en caso de ser variables o arrays multidimencionales array("pepito"=>array("casado" => "si"))
* @return myTemplate
*/
public function assigns($array){}
/**
* @param string $var
* @param mixed $value el valor puede ser cualquiera
*/
public function assign($var,$value){}
public function & scope($array){}
/**
* @param string $var
* @param mixed $value el valor puede ser cualquiera
*/
public function ref($var,& $value){}
/**
* @param array $array
* @param mixed $value el valor puede ser cualquiera
*/
public function refs(&$array){}
/**
* @param string $var
* @param mixed $value el valor puede ser cualquiera
*/
public function concatAssign($var,$value){}
/**
* @param string $var
* @param mixed $value el valor puede ser cualquiera
*/
public function concatVar($var,$value){}
public function & getData($name_var = null,$return_default = false){}
public function & getAllData($is_assigns = true){}
/**
* metodo magico que hace que las variables ingresadas a la clase sean procesadas de $plantilla->VARIABLE = "valor cualquier"
* como si fuese $plantilla->assign('VARIABLE',"valor cualquier");
*/
public function __set($var,$value){}
/**
* @param string $var
* @param mixed $value el valor puede ser cualquiera
*/
public function set($var,$value){}
/**
* @param string $all acepta 'all','vars','assigns' , por defecto esta en all para limpiar todas la variables
* @return plantilla_tuadmin retorna la misma clase para seguir procesando
*/
public function erase($all = 'all'){}
/**
*@param string $__file_name Nombre del archivo correspondiente
*@param bool $__parse_from_memory parsear archivo desde memoria o desde disco, es recomendable desde disco si el template es llamado 1 vez
*@return myTemplate
*/
public function parse($__file_name,$__parse_from_memory = false){}
/**
* @param string $__file_name el nombre del archiv
* @param boolean $__short_variables activar o no variables cortas {VARIABLE} como si fuese <?=$VARIABLE?>
* @return string retorna la cadena o la plantilla ya procesada
*/
public function render($__short_variables = false) {}
}
myTemplate.class.php codigo minimizado y funcional
Código PHP :
<?php
/**
* @version 1.0.6
* @author tuadmin
* @copyright 2012
* @file mytemplate
* + fix error Eval
* + add functioon _showDebug
* --1.0.4--
* + fix control for existence of dirs
* --1.0.3--
* + add function refs array by reference ->refs ($array)
* + add function ref variables by reference ->ref('alias',$variable)
* --1.0.2--
* + add support for multipath,array() or string PATH
* + add function concatAssing
* + add function concatVar
* --1.0.1--
* + add Comunicators templates variables $__URL__ or $this->URL
* + add static function newInstance()
*/
if(!defined('MODE_DEBUG')){define('MODE_DEBUG',true);} if(class_exists('MyErrorException')){class myTemplateException extends MyErrorException{}} else{ class myTemplateException extends Exception{ public function __construct($vMessage, $vCode, $vFile, $vLine, $arContext = null) { parent::__construct($vMessage, $vCode); } public function showDebug(){} }} class myTemplate { private $vars = array(); private $assigns = array(); private $PATH; private $URL = '.'; private $URLS = array(); private $PATHS = array(); private $_extension = '.tpl.php'; private $_level = 0; private $_levels = array(); private $_template_eval = array(); private $_last_error = ''; private $_is_multipath = false; const DEBUG = MODE_DEBUG; static public function newInstance($path ,$extension = false) { return new self($path ,$extension); } public function __construct($path ,$extension = false) { $this->_parserPathUrl($path); $this->_extension = $extension? $extension : $this->_extension; } private function _parserPathUrl($path) { if(is_array($path)) { $this->PATHS = array(); $this->_is_multipath = true; if(isset($path[0])){ foreach($path as $dir ) { if(!is_dir($dir)){continue;} $this->PATHS[] = $dir; $this->URLS[] = '.'; } }else { foreach($path as $dir => $url) { if(!is_dir($dir)){continue;} $this->PATHS[] = $dir; $this->URLS[] = $url; } } } $this->PATH = $path; } public function setUrl($url) { $this->URL = $url; } public function vars($array) { $this->vars[$this->_level] = $array; return $this; } public function _($var,$value) { $this->vars[$this->_level][$var] = $value; return $this; } public function assigns($array) { $this->assigns[$this->_level] = $array; return $this; } public function assign($var,$value) { $this->assigns[$this->_level][$var] = $value; return $this; } public function & scope($array) { if(is_string($array)) { $array = func_get_args(); } $this->assigns[$this->_level] = array_fill_keys($array,null); return $this->assigns[$this->_level]; } public function ref($var,& $value) { $this->assigns[$this->_level][$var] =& $value; return $this; } public function refs(&$array) { $this->assigns[$this->_level]= & $array; return $this; } public function concatAssign($var,$value) { if(isset($this->assigns[$this->_level][$var]) && is_string($this->assigns[$this->_level][$var]) ) { $this->assigns[$this->_level][$var] .= $value; } else { $this->assigns[$this->_level][$var] = $value; } return $this; } public function concatVar($var,$value) { if(isset($this->vars[$this->_level][$var]) && is_string($this->vars[$this->_level][$var])) { $this->vars[$this->_level][$var] .= $value; } else { $this->vars[$this->_level][$var] = $value; } return $this; } public function & getData($name_var = null,$return_default = false) { $return = false; if($name_var == null) { return $this->assigns[$this->_level]; } else { if(isset($this->assigns[$this->_level][$name_var])) { return $this->assigns[$this->_level][$name_var]; } else if($this->vars[$this->_level][$name_var]) { return $this->vars[$this->_level][$name_var]; } } return $return; } public function & getAllData($is_assigns = true) { $return = false; if($is_assigns) { return $this->assigns[$this->_level]; } else { return $this->vars[$this->_level]; } return false; } public function __set($var,$value) { $this->assign($var,$value); } public function __call($name,$args) { $name_var = array_shift($args); switch(strtolower($name)) { case 'getdata' : return isset($this->vars[$name_var]) ? $this->vars[$name_var] : $this->assigns[$name_var]; } return false; } public function set($var,$value) { is_string($value) ? $this->_($var,$value) : $this->assign($var,$value); return $this; } public function __get($var_name) { switch(true) { case isset($this->vars[$this->_level][$var_name]):return $this->vars[$var_name]; case isset($this->assigns[$this->_level][$var_name]):return $this->assigns[$var_name]; default:false; } } public function erase($all = 'all') { switch($all) { default : ; case 'all' : $this->vars[$this->_level] = array(); case 'assigns' : $this->assigns[$this->_level] = array(); break; case 'vars' : $this->vars[$this->_level] = array(); break; } return $this; } public function parse($__file_name,$__parse_from_memory = false) { $this->_level++; $this->_levels[$this->_level] = $__file_name ; $this->vars[$this->_level] = array(); $this->assigns[$this->_level] = array(); if($__parse_from_memory && !isset($this->_template_eval[$__file_name])) { $path = $this->__return_path_file($__file_name); $this->_template_eval[$__file_name] = $path? file_get_contents($path) :false; } return $this; } public function render($__short_variables = false) { if(self::DEBUG ) { try { return self::DEBUG ? $this->render_debug_on($__short_variables) : $this->render_debug_off($__short_variables); }catch(Exception $e){echo $e->getMessage() . $e->getLine(). $e->getFile();} } else { return $this->render_debug_off($__short_variables); } } protected function render_debug_on($__short_variables) { ob_start (); $__NAME__ = & $this->_levels[$this->_level]; if(isset($this->_template_eval[ $__NAME__])) { $__ERROR__ = is_string($this->_template_eval[ $__NAME__]) ? $this->load_memory() :$this->_set_error('file'); } else { $__FILE__ = $this->__return_path_file($__NAME__); $__ERROR__ = $__FILE__? $this->load_disk_debug_on($__FILE__) :$this->_set_error('file'); } if($__ERROR__) { throw new Exception("\nTemplate not Found :" . $this->PATH . DIRECTORY_SEPARATOR . $__NAME__ .$this->_extension,404); return false; } return false !== $__short_variables ? $this->parser_vars(ob_get_clean(),$__short_variables).$this->reduce_level() : ob_get_clean().$this->reduce_level(); } protected function render_debug_off($__short_variables) { ob_start (); $__NAME__ = & $this->_levels[$this->_level]; if(isset($this->_template_eval[ $__NAME__])) { $__ERROR__ = is_string($this->_template_eval[ $__NAME__]) ? $this->load_memory() :$this->_set_error('file'); } else { $__FILE__ = $this->__return_path_file($__NAME__); $__ERROR__ = $__FILE__? $this->load_disk($__FILE__) :$this->_set_error('file'); } if($__ERROR__) { return false; } return false !== $__short_variables ? $this->parser_vars(ob_get_clean(),$__short_variables).$this->reduce_level() : ob_get_clean().$this->reduce_level(); } protected function _set_error($type) { switch($type) { case 'file' :$msg = 'template file not found'; break; case 'parse' :$msg = 'template syntax error'; break; default:$msg = 'Unknown Error'; } $this->_last_error = $msg; return true; } protected function load_memory() { extract($this->vars[$this->_level],EXTR_REFS); extract($this->assigns[$this->_level],EXTR_REFS); $__URL__ = & $this->URL; $__PATH__ = & $this->PATH; try{ if(@eval ( '?>' . $this->_template_eval[ $this->_levels[$this->_level] ] ) === false && self::DEBUG) { $r = error_get_last(); throw new myTemplateException($r['message'], $r['type'], $__FILE__, $r['line']); $this->_set_error('parse'); } }catch(myTemplateException $e) {die($e->showDebug());} } protected function reduce_level() { $this->_level--; } protected function load_disk($__FILE__) { extract($this->vars[$this->_level],EXTR_REFS); extract($this->assigns[$this->_level],EXTR_REFS); $__URL__ = & $this->URL; $__PATH__ = & $this->PATH; include $__FILE__; } protected function load_disk_debug_on($__FILE__) { extract($this->vars[$this->_level],EXTR_REFS); extract($this->assigns[$this->_level],EXTR_REFS); $__URL__ = & $this->URL; $__PATH__ = & $this->PATH; $__contenido__ = file_get_contents($__FILE__); try{ if(@eval ( '?>' . $__contenido__ ) === false) { $r = error_get_last(); throw new myTemplateException($r['message'], $r['type'], $__FILE__, $r['line']); }}catch(myTemplateException $e) {die($e->showDebug());} } protected function parser_vars($data,$reemplazar = true) { $var_referencia = array(); if(is_array($reemplazar)) { foreach( $reemplazar as $key => $value) { $var_referencia['{'.$key.'}'] = & $reemplazar[$key]; } } else { foreach( $this->vars[$this->_level] as $key => $value) { $var_referencia['{'.$key.'}'] = & $this->vars[$this->_level][$key]; } } return strtr($data , $var_referencia); } protected function __return_path_file($__NAME__) { if($this->_is_multipath) { foreach($this->PATHS as $key => $PATH) { if(file_exists($PATH . DIRECTORY_SEPARATOR . $__NAME__ .$this->_extension)) { $this->URL = $this->URLS[$key]; $this->PATH = $this->PATHS[$key]; return $PATH . DIRECTORY_SEPARATOR . $__NAME__ .$this->_extension; } else { $this->URL = $this->URLS[$key]; $this->PATH = $this->PATHS[$key]; } } } else { return realpath($this->PATH . DIRECTORY_SEPARATOR . $__NAME__ .$this->_extension); } return false; } }