Questa è la classe:
template_parser.php
è in php4.. il funzionamento è abbastanza semplice.. si tratta di creare un file di template anche html tipo: template.htmCodice PHP:<?php
class template_parser {
var $output;
function template_parser($tags = array(), $cache_file = 'cache.txt', $durata = 0, $template_file = 'index.htm') {
if (!$this->output = $this->leggi_cache($cache_file, $durata)) {
(file_exists($template_file)) ? $this->output = file_get_contents($template_file) : die('Errore: File ' . $template_file . ' not found');
$this->parser($tags, $cache_file);
}
}
function parser($tags, $cache_file) {
if (count($tags) > 0) {
foreach ($tags AS $k=>$v) {
$v = (file_exists($v)) ? $this->parseFile($v) : $v;
$this->output = str_replace('{' . $k . '}', $v, $this->output);
}
$this->scrivi_cache($cache_file, $this->output);
} else {
die('Errore: Non ci sono tag assegnati per rimpiazzare i valori');
}
}
function mostra() {
return $this->output;
}
function parseFile($file) {
ob_start();
include($file);
$contenuto = ob_get_contents();
ob_end_clean();
return $contenuto;
}
function leggi_cache($cache_file, $durata) {
if (file_exists($cache_file) && filemtime($cache_file) > (time() - $durata)) {
return file_get_contents($cache_file);
}
return FALSE;
}
function scrivi_cache($cache_file, $contenuto) {
$fp = fopen($cache_file, 'w');
fwrite($fp, $contenuto);
fclose($fp);
}
}
?>
per utilizzare la classe crei un foglio index.php cosi:Codice PHP:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{titolo}</title>
<link rel="stylesheet" style="text/css" media="all" href="***.css" />
</head>
<body>
<div id="testata">{logo}</div>
<div id="menu">{menu}</div>
<div id="contenitore">
<div id="colonna">{colonna}</div>
<div id="contenuti">{com_content}</div>
</div>
<div id="footer">{footer}</div>
</body>
</html>
l'array tags ha come chiavi le parole contenute nel file template tra {}.. e come valori dei file che vuoi importare di qualsiasi tipo..Codice PHP:<?php
error_reporting(E_ALL);
require_once('template_parser.php');
$tags = array('logo' => 'logo.inc',
'menu' => 'include/menu.inc.php',
'colonna' => 'colonna.php',
'com_content' => 'switch.php',
'footer' => 'footer.inc'
);
$temp =& new template_parser($tags, 'cache.txt', 3600, 'template.htm');
echo $temp->mostra();
?>
Il numero 3600 ti dice in secondi ogni quanto vuoi che il file di cache.txt venga aggiornato, in questo caso un'ora..
Tieni per buono anche il consiglio di robertes cmq..
![]()

Rispondi quotando