<?php
class tpl
{
var $contenu;
var $ajout = array();
var $ajout_bloc = array();
function tpl($page = index.tpl)
{
if(!file_exists($page))
{
$page = index.tpl;
}
$chemin = $page;
$template = fopen($chemin, r+);
$this->contenu .= fread($template, filesize($chemin));
fclose($template);
}
function ajout($item, $valeur)
{
$this->ajout[$item] = $valeur;
}
function ajout_bloc($bloc, $array)
{
$this->ajout_bloc[$bloc][] = $array;
}
function fin()
{
foreach($this->ajout as $item => $valeur)
{
$this->contenu = str_replace({.$item.}, $valeur, $this->contenu);
}
foreach($this->ajout_bloc as $bloc => $array)
{
preg_match(!<!-- Debut .$bloc. -->(.*)<!-- Fin .$bloc. -->!isU, $this->contenu, $res) or $res = array(, );
$remplacement = ;
foreach($array as $numero => $arrayvals)
{
$a_ajoute = $res[1];
foreach($arrayvals as $item => $valeur)
{
if(is_array($valeur))
{
preg_match(!<!-- Debut .$bloc._.$item. -->(.*)<!-- Fin .$bloc._.$item. -->!isU, $res[1], $res2) or $res2 = array(, );
$remplacement2 = ;
foreach($valeur as $numero2 => $arrayvals2)
{
$a_ajoute2 = $res2[1];
foreach($arrayvals2 as $item2 => $valeur2)
{
$a_ajoute2 = str_replace({.$bloc._.$item._.$item2.}, $valeur2, $a_ajoute2);
}
$remplacement2 .= $a_ajoute2;
}
$res[1] = str_replace($res2[0], $remplacement2, $res[1]);
}
else
{
$a_ajoute = str_replace({.$bloc._.$item.}, $valeur, $a_ajoute);
}
}
$remplacement .= $a_ajoute;
}
$this->contenu = str_replace($res[0], $remplacement, $this->contenu);
}
return($this->contenu);
}
}
?>
<?php
require(templates.class.php);
$tpl = new tpl(index.tpl);
$tpl->ajout(Titrepage, Test de templates);
$tpl->ajout_bloc(Paragraphe, array(Titre => Titre du paragraphe, Contenu => Petit test :p, Liste => array(array(Nom => Test))));
$tpl->ajout_bloc(Paragraphe, array(Titre => Titre du paragraphe, Contenu => Petit test :p, Liste => array(array(Nom => Test))));
echo $tpl->fin();
?>
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<title>{Titrepage}</title>
</head>
<body>
<!-- Debut Paragraphe -->
<h1>{Paragraphe_Titre}</h1>
<p>{Paragraphe_Contenu}</p>
<ul>
<!-- Debut Paragraphe_Liste -->
<li>{Paragraphe_Liste_Nom}</li>
<!-- Fin Paragraphe_Liste -->
</ul>
<!-- Fin Paragraphe -->
</body>
</html>
Array
(
[Paragraphe] => Array
(
[0] => Array
(
[Titre] => Titre du paragraphe
[Contenu] => Petit test :p
[Liste] => Array
(
[0] => Array
(
[Nom] => Test
)
)
)
[1] => Array
(
[Titre] => Titre du paragraphe
[Contenu] => Petit test :p
[Liste] => Array
(
[0] => Array
(
[Nom] => Test
)
)
)
)
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">
<title>Test de templates</title>
</head>
<body>
<h1>Titre du paragraphe</h1>
<p>Petit test :p</p>
<ul>
<!-- Debut Paragraphe_Liste -->
<li>{Paragraphe_Liste_Nom}</li>
<!-- Fin Paragraphe_Liste -->
</ul>
<h1>Titre du paragraphe</h1>
<p>Petit test :p</p>
<ul>
<li>Test</li>
</ul>
</body>
</html>
Nous - Plan du site - Informations légale - Contact - © Graphiks.net