Encore un systeme de template, de meme syntaxe que phplib ou phpbb :
mais il se comporte mieux sur les blocks optionnels.
Il faut utiliser à fond les tableaux php qui sont très pratiques (et var_dump..)
exemple : à partir de deux contenus, un chaine html et d'un tableau :
print filtpl_compile(
"{a}<!-- BEGIN t -->{e}<!-- END t -->",
array(a => "P",t => array(0=>array(e=>"H"),1=>array(e=>"P"))));
affiche "PHP"
Un block peut être considéré comme un IF ou un LOOP suivant que la valeur est un null, un array(), un tableau de key->val, ou un tableau numérique [i]->val.
Source / Exemple :
<?
// FILNET GUY KASTENBAUM 15 MARS 2006 V1.1 , ajout tags: begin end
// FILNET GUY KASTENBAUM 10 NOVEMBRE 2005 , V1.0
// filtpl.php un template aussi bien que les autres
//$tt=array(a => "aaa",b => "bbb",t => array(0=>array(e=>"eee"),1=>array(e=>"fff")));
//$tf="111{a}<!-- BEGIN t -->{e}<!-- END t --><!-- BEGIN u -->u{e}u<!-- END u -->222{b}";
//print filtpl_compile($tf,$tt);
//recursive function filtpl_compile($filtpl_tf,$filtpl_tt)
//input string,tree / ouput : compiled string
// filtpl_tf = string of the template file
// filtpl_tt = array of values or subarrays
function filtpl_compile($filtpl_tf,$filtpl_tt){
if (!$filtpl_tt) return("");//unwanted branch I presume
if (!is_array($filtpl_tt)) return($filtpl_tf);//like a bug
if (preg_match('#<!-- BEGIN (\w+) -->#i', $filtpl_tf , $filtpl_m , PREG_OFFSET_CAPTURE))
{
$filtpl_before=substr($filtpl_tf,0,$filtpl_m[0][1]);
$filtpl_tag=$filtpl_m[1][0];//tag for first begin
$filtpl_after=substr($filtpl_tf,$filtpl_m[0][1]+strlen("<!-- BEGIN $filtpl_tag -->"));
if (!preg_match("#<!-- END $filtpl_tag -->#i",$filtpl_after, $filtpl_m , PREG_OFFSET_CAPTURE)) die("begin $filtpl_tag with no end");
$filtpl_loop=substr($filtpl_after,0,$filtpl_m[0][1]);
$filtpl_after=substr($filtpl_after,$filtpl_m[0][1]+strlen("<!-- END $filtpl_tag -->"));
//string is splitted in before.loop.after -- lets recurse!
$filtpl_tf=$filtpl_before;
$filtpl_tf.=$filtpl_tt[$filtpl_tag]["begin"];
//is it a loop or just a if ? (ugly test : if [0] exists it is supposed to be a loop)
if ($filtpl_tt[$filtpl_tag][0])
foreach ($filtpl_tt[$filtpl_tag] as $filtpl_tt_sub) //lets loop
$filtpl_tf.=filtpl_compile($filtpl_loop,$filtpl_tt_sub);
else // lets do the unique loop
$filtpl_tf.=filtpl_compile($filtpl_loop,$filtpl_tt[$filtpl_tag]);
$filtpl_tf.=$filtpl_tt[$filtpl_tag]["end"];
$filtpl_tf.=filtpl_compile($filtpl_after,$filtpl_tt); //recurse the afterloop
}
//substitute known values of root level
foreach ($filtpl_tt as $filtpl_key=>$filtpl_val)
if (!is_array($filtpl_val))
$filtpl_tf=preg_replace("/\{$filtpl_key\}/i",$filtpl_val,$filtpl_tf);
$filtpl_tf=preg_replace("/\{\w+\}/i","",$filtpl_tf);//zap unknown values
return($filtpl_tf);
}
/*--- example , just for the eyes ---
//$tf=join("",file("example.htm"));
$tf="{title}
<!-- BEGIN prd_list -->
{column_one} {column_two}
<!-- BEGIN if_ok -->{msg}<!-- END if_ok -->
<!-- END prd_list -->
";
$tt=array();//create a string and an array
$tt["titre"]= "hello world";//root level
$tt["prd_list"]= array();//loop
$list=mysql_query("select column_one,column_two from a_table");
while ($rs=mysql_fetch_array($list)){
$tt_prd_list= array();//loop level data
foreach (array_keys($rs) as $chp)
$tt_prd_list[$chp]=$rs[$chp]; //fill each items
if ($rs["column_one"]==1) // "if" clause
$tt_prd_list["if_ok"]=array("msg"=>"OK");
array_push($tt["prd_list"],$tt_prd_list);//fill loop
}
//debug : print"<pre>";print_r($tt);print"</pre>";
print filtpl_compile($tf,$tt);//pparse in other langages
?>
Conclusion :
que manque-t-il ? non il ne faut pas surcharger ce code par des options de formatage dont le graphiste se fout, ou des fonctions d'enrobage. éventuellement il manque une fonction else.
j'adore ce code :-) il marche en prod sur pas mal de mes sites.
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.