Ecco la classe, che peraltro l'ho copiata da un post qui sul forum... non mi ricordo l'autore
Codice PHP:
// Gestisce i templates
//
// I templates sono fondamentalmente pagine html con possibilità di sostituire
// dinamicamente alcune entità.
// Le entità sono
// - parametri : {nome_parametro}
// - blocchi :
//
//
// ...
// blocco
// ...
//
//
// Tutto quello tra beg ed end fa parte del blocco
// Il blocco può contenere codice HTML, parametri o altri blocchi
// Essendo racchiuso in un commento html non genera output
//
// In entrambi i casi occorre che non ci siano spazi tra il tag di apertura
// e quello di chiusura( e {...} )
//
class zz_template
{
// carica template da file
function loadfile($templatefilename){
$content="";
if(file_exists($templatefilename)){
if($hfile = fopen($templatefilename, "r")){
$content = fread($hfile, filesize($templatefilename));
fclose($hfile);
}
}
$this->loadstring($content);
}
// carica template da stringa
function loadstring($template){
$this->template = $template;
}
// restituisce il codice prodotto dal template
function get(){
return($this->template);
}
// effettua la sostituzione di tutti i parametri (a prescindere dal blocco)
// sostituisce tutti i parametri con i valori
// i nomi dei parametri e i valori sono specificati rispettivamente dalle
// chiavi e dai valori dell'array $params
// Se bremove è false aggiunge una copia del parametro template
// dopo la sostituzione in modo da rendere possibili sostituzioni iterative
//
function parse($params, $bremove = true){
if($params){
$keys = array_keys($params);
for($i=0; $i<count($keys); $i++){
$param_name = $keys[$i];
$param_value = $params[$param_name];
$this->replace($param_name, $param_value, $bremove);
}
}
return($this->template);
}
// come parse ma effettua la sostituzione solo sul blocco block
// sono sostituiti tutti e solo i parametri che si trovano sia nel blocco che
// nelle chiavi di $params
// Se bremove è false viene effettuata una copia del template-blocco in modo
// da rendere possibili sostituzioni iterative
// Se ci sono parametri con lo stesso nome ma fuori dal blocco o in altri blocchi
// non vengono sostituiti
//
function parse_block($block, $params, $bremove = true){
$block_str = $this->getBlock($block);
$block_content = $this->getBlockContent($block);
$template = new zz_template();
$template->loadstring($block_content);
$template->parse($params, true);
$text = $template->get().($bremove?"":$block_str);
$this->template = str_replace($block_str, $text, $this->template);
}
// Rimuove il blocco template block
function remove_block($block){
$this->template = str_replace($this->getBlock($block), "", $this->template);
}
////////////////////////////////////////////////////////////
// metodi privati
function replace($param_name, $param_value, $bremove = true){
$param_tag = "{".$param_name."}";
$text = $param_value.($bremove?"":$param_tag);
$this->template = str_replace($param_tag, $text, $this->template);
}
// prende template (inclusi i tag di beg/end)
function getBlock($block){
$startblock_str = "";
$endblock_str = "";
if(($s = strpos($this->template, $startblock_str))===false) return "";
if(($e = strpos($this->template, $endblock_str)) ===false) return "";
return(substr($this->template, $s, $e-$s+strlen($endblock_str)));
}
// prende solo la parte interna al blocco
function getBlockContent($block){
$startblock_str = "";
$endblock_str = "";
if(($s = strpos($this->template, $startblock_str))===false) return "";
if(($e = strpos($this->template, $endblock_str)) ===false) return "";
$offset = strlen($startblock_str);
$s += $offset;
return(substr($this->template, $s, $e-$s));
}
// template e prodotto
var $template;
}
Come si può aggiornare per far si che includa pagine?