Mais un bon code vaut mieux qu'un long discours:
<?php
class template
{
private $path = '';
public $tpl = '';
private $temp = '';
private $parse = '';
public function __construct($path)
{
if (is_dir ($path)) $this->path = $path;
else exit;
}
public function get ($tplname)
{
$this->tpl = $this->path.'header.tpl';
if (file_exists ($this->tpl))
{
$this->temp = fopen ($this->tpl, 'rb');
$code = fread ($this->temp, filesize ($this->tpl));
return htmlspecialchars ($code);
fclose ($this->temp);
}
else echo 'le template '.$this->tpl.' n\'existe pas!';
}
public function output ($tplname, $vars=array())
{
$this->tpl = $this->path.'header.tpl';
if (file_exists ($this->tpl))
{
$this->temp = fopen ($this->tpl, 'rb');
$this->parse = fread ($this->temp, filesize ($this->tpl));
$this->parse = $this->parsevar ($this->parse, $vars);
print $this->parse;
fclose ($this->temp);
}
else echo 'le template '.$this->tpl.' n\'existe pas!';
}
private function parsevar ($tplname, $code=array())
{
if ($code != '')
{
foreach ($code as $item)//c'est le code que je trouve pas!!!
{
$preg = '$code[' . "*" . ']';
$var = preg_replace("#{(.*)}#", $preg, $code);
}//fin du code que je trouve pas
eval( "\$var= \"$var\";" );
return $var;
}
else exit;
}
public function __destruct()
{
}
};
?>
Et ce qui me donne pour résultat, quand j'appelle un template: array(), et rien d'autre, même pas le titre de la page EDIT: j'ai finalement trouvé quelque chose de plus clair:
private function parsevar ($parse, $code=array())
{
if ($code != '')
{
for ($i=0, $imax=count($code); $i<$imax; $i++)
{
foreach ($code[$i] as $constant => $data) //l.50
{
$this->parse = preg_replace ('`{'.$constant.'}`', $data, $parse);
}
}
return $this->parse;
}
else exit;
}
Ce qui me renvoit maintenant comme erreur:Warning: Invalid argument supplied for foreach() in D:\web\test\includes\class_tpl.php on line 50