ciao a tutti oggi ho deciso di cominciare a creare una homepage di un mio sito ma mi sono fermato ad un punto cioè quando ho usato la class template scaricata da un sito.
In pratica il mio sito è formato da una pagina con elementi fissi che non cambiano. menu e logo mentre nel cuore della pagina vorrei che a seconda dei link che si cliccano sul menu comparissero i risultati per questo ho pensato che la classe template sia la classe che mi serva per il mio sito ma non riesco a capire come unire i due template tpl: il file "statico" menu + logo e quello dei contenuti.
Ecco i file index.php:
Codice PHP:
<?php
try {
/*** include the class definition ***/
include "template.class.php";
/*** create a new instance ***/
$template = new template;
/*** turn cache on or off ***/
$template->setCaching(true);
/*** set the template directory ***/
$template->setTemplateDir("templates");
/*** set the cache directory ***/
$template->setCacheDir("my_cache");
/*** set the cache lifetime in seconds ***/
$template->setCacheLifetime(3600);
/*** show the template ***/
$template->display("home.tpl.html", $id);
/*** clear the cache ***/
$template->clearCache();
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
e class.template.php
Codice PHP:
<?php
class Template{
private $variables = array();
private $template_dir = null;
private $caching = false;
private $cache_dir = 'cache';
private $cache_lifetime = 300;
public function __set($name, $value){
$this->variables[$name] = $value;
}
public function getVars(){
$variables = array_keys($this->variables);
return !empty($variables) ? $variables : false;
}
/**
*
* Outputs the final template output
*
* Fetches the final template output, and echoes it to the browser.
*
* @param string $file Filename (with path) to the template you want to output
*
* @param string $id The cache identification number/string of the template you want to fetch
*
* @access public
*
* @return void
*
* @see fetch
*
*/
public function display($file, $id = null){
echo $this->fetch($file, $id);
}
/**
* Fetch the final template output and returns it
*
* @param string $template_file Filename (with path) to the template you want to fetch
*
* @param string $id The cache identification number/string of the template you want to fetch
*
* @access private
*
* @return string Returns a string on success, FALSE on failure
*
* @see display
*
*/
private function fetch($template_file, $id = null){
/*** if the template_dir property is set, add it to the filename ***/
if (!empty($this->template_dir))
{
$template_file = realpath($this->template_dir) . '/' . $template_file;
}
/*** get the cached file contents ***/
if ($this->caching == true && $this->isCached($template_file, $id))
{
$output = $this->getCache($template_file, $id);
}
else
{
$output = $this->getOutput($template_file);
/*** create the cache file ***/
if ($this->caching == true)
{
$this->addCache($output, $template_file, $id);
}
}
return isset($output) ? $output : false;
}
/**
*
* Fetch the template output, and return it
*
* @param string $template_file Filename (with path) to the template to be processed
*
* @return string Returns a string on success, and FALSE on failure
*
* @access private
*
* @see fetch, display
*
*/
private function getOutput($template_file){
/*** extract all the variables ***/
extract($this->variables);
if (file_exists($template_file))
{
ob_start();
include($template_file);
$output = ob_get_contents();
ob_end_clean();
}
else
{
throw new Exception("The template file '$template_file' does not exist");
}
return !empty($output) ? $output : false;
}
/**
*
* Sets the template directory
*
* @param string $dir Path to the template dir you want to use
*
* @access public
*
* @return void
*
*/
public function setTemplateDir($dir){
$template_dir = realpath($dir);
if (is_dir($template_dir))
{
$this->template_dir = $template_dir;
}
else
{
throw new Exception("The template directory '$dir' does not exist");
}
}
/**
*
* Sets the cache directory
*
* @param string $dir Path to the cache dir you want to use
*
* @access public
*
* @return void
*
* @see setCacheLifetime
*
*/
function setCacheDir($dir){
$cacheDir = realpath($dir);
if (is_dir($cacheDir) && is_writable($cacheDir))
{
$this->cache_dir = $cacheDir;
}
else
{
throw new Exception("The cache directory '$cacheDir' either does not exist, or is not writable");
}
}
/**
* Sets how long the cache files should survive
*
* @param INT $seconds Number of seconds the cache should survive
*
* @access public
*
* @return void
*
* @see setCacheDir, isCached, setCaching
*
*/
public function setCacheLifetime($seconds=0){
$this->cache_lifetime = is_numeric($seconds) ? $seconds : 0;
}
/**
* Turn caching on or off
*
* @param bool $state Set TRUE turns caching on, FALSE turns caching off
*
* @access public
*
* @return void
*
* @see setCacheLifetime, isCached, setCacheDir
*
*/
public function setCaching($state){
if (is_bool($state))
{
$this->caching = $state;
}
}
/**
* Checks if the template in $template is cached
*
* @param string $file Filename of the template
*
* @param string $id The cache identification number/string of the template you want to fetch
*
* @access public
*
* @return bool
*
* @see setCacheLifetime, setCacheDir, setCaching
*
*/
public function isCached($file, $id = null){
$cacheId = $id ? md5($id . basename($file)) : md5(basename($file));
$filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
if (is_file($filename))
{
clearstatcache();
if (filemtime($filename) > (time() - $this->cache_lifetime))
{
$isCached = true;
}
}
return isset($isCached) ? true : false;
}
/**
* Makes a cache file. Internal method
*
* @param string $content The template output that will be saved in cache
*
* @param string $file The filename of the template that is being cached
*
* @param string $id The cache identification number/string of the template you want to fetch
*
* @access private
*
* @return void
*
* @see getCache, clearCache
*/
private function addCache($content, $file, $id = null){
/*** create the cache id ***/
$cacheId = $id ? md5($id . basename($file)) : md5(basename($file));
/*** create the cache filename ***/
$filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
/*** create the directory name for the cache file ***/
$directory = $this->cache_dir . '/' . $cacheId;
/*** create the cache directory ***/
@mkdir($directory, 0775);
/*** write to the cache ***/
if(file_put_contents($filename, $content) == FALSE)
{
throw new Exception("Unable to write to cache");
}
}
/**
* Returns the content of a cached file
*
* @param string $file The filename of the template you want to fetch
*
* @param string $id The cache identification number/string of the template you want to fetch
*
* @access private
*
* @return string Cached content on success, FALSE on failure
*
* @see addCache, clearCache
*
*/
private function getCache($file, $id = null){
$cacheId = $id ? md5($id . basename($file)) : md5(basename($file));
$filename = $this->cache_dir . '/' . $cacheId . '/' . basename($file);
/*** read the cache file into a variable ***/
$content=file_get_contents($filename);
return isset($content) ? $content : false;
}
/**
* Deletes all of the stored cache files
*
* @access public
*
* @return void
*
* @see addCache, getCache
*
*/
public function clearCache(){
$cacheDir = realpath($this->cache_dir);
$this->delDir($cacheDir);
}
/**
* Remove files and folders recursively.
* WARNING: It does not care what directory $dir is.
*
* @param string $dir directory to remove files and folders from
*
* @access private
*
* @return void
*
* @see clearCache
*
*/
private function delDir($dir) {
/*** perhaps a recursiveDirectoryIteratory here ***/
$deleteDir = realpath($dir);
if ($handle = opendir($deleteDir))
{
while (false !== ($file = readdir($handle)))
{
if ($file != '.' && $file != '..')
{
if (is_dir($deleteDir . '/' . $file))
{
$this->delDir($deleteDir . '/' . $file);
if(is_writable($deleteDir . '/' . $file))
{
rmdir($deleteDir . '/' . $file);
}
else
{
throw new Exception("Unable to remove Directory");
}
}
elseif(is_file($deleteDir . '/' . $file))
{
if(is_writable($deleteDir . '/' . $file))
{
unlink($deleteDir . '/' . $file);
}
else
{
throw new Exception("Unable to unlink $deleteDir".'/'."$file");
}
}
}
}
closedir($handle);
}
}
} /*** end of class ***/
?>