Verfasst: 07.11.2006 17:40
@Olli Oberhausen: Danke ich schaue mir das bei Gelegenheit mal an. Aber ich denke für das, was ich machen will, ist die aktuelle Lösung ausreichend.
Gruß
Thorsten
Gruß
Thorsten
phpBB.de - Die deutsche phpBB-Community
https://www.phpbb.de/community/
Wenn das deine denkweise ist - ok....Sgt. Absolom hat geschrieben:Aber ich denke für das, was ich machen will, ist die aktuelle Lösung ausreichend.
Gruß
Thorsten
Nur ein tipp von mir, wenn du nacher nicht alles umstellen wilst.Wie gesagt, ich möchte ein eigenes kleines CMS programmieren
Code: Alles auswählen
<?php
class Template
{
//
// private variables
//
private $assigns = array(); // saves the added arrays/strings
private $file = array(); // saves the added file
private $root; // saves the root directory of the templates dir
/**
*
* Constructor and method to set the templates root dir
*
* @access puplic
* @param strin $dir
* @return true/false
*
**/
function Template($dir = '.')
{
if (!dir($dir))
{
die("Template->Template(): Can´t set any root dir for $dir");
}
$this->root = $dir;
return true;
}
/**
*
* function to set handle and file for display
*
* @access puplic
* @param string $handle
* @param string $tpl_file
* @return true
*
**/
function set_filename($handle, $tpl_file)
{
if (empty($handle))
{
die('Template->set_filename(): Can´t set a filename for an empty handle');
}
if (empty($tpl_file))
{
die("Template->set_filename(): Can´t set an empty file for $handle");
}
if (!file_exists($tpl_root . $tpl_file))
{
die("Template->set_filename(): File $file doesn´t exists in $tpl_root$tpl_file");
}
$this->file[$handle] = $tpl_file;
return true;
}
/**
*
* function to assign normaly key´s and values
*
* @access puplic
* @param string $key
* @param string $value
* @return true
*
**/
function assign($key, $value)
{
if (empty($key))
{
die('Template->assign(): Can´t add an empty Key');
}
if (empty($value))
{
die("Template->assign(): Can´t add an empty Value for Key: $key");
}
$this->assigns[$key] = $value; // adds key and value
return true;
}
/**
*
* function to assign arrays
*
* @access puplic
* @param array $vars_array
* @return true
*
**/
function assign_vars($vars_array)
{
if (!is_array($vars_array) OR is_object($vars_array))
{
die('Template->assign_vars(): You have to add an array');
}
while (list($key, $value) = each($vars_array))
{
$this->assigns[$key] = $value; // adds key and value
}
return true;
}
/**
*
* function to compile and display the template which was assigns by set_filename
*
* @access puplic
* @param string $handle
* @return string $code
*
**/
function display($handle)
{
if (empty($handle))
{
die('Template->display(): Can´t display an empty handle');
}
if (!in_array($handle, $this->file))
{
die("Template->display(): You have to add this handle($handle) in the method: set_filename");
}
$code = file($this->root . $this->file[$handle]);
$code = implode('', $code);
$code = str_replace('\\', '\\\\', $code);
$code = str_replace('\'', '\\\'', $code);
$code_lines = explode("\n", $code);
$line_count = sizeof($code_lines);
for ($i = 0; $i < $line_count; $i++)
{
if (preg_match('#\{([a-z0-9\-_])\}#is', $code_lines[$i], $m))
{
$code = str_replace("{$m[1]}", $this->assigns[$m[1]], $code);
}
}
echo $code;
return true;
}
}
?>