01.
02.
03.
04.
05.
06.
07.
08.
09.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
171.
172.
173.
174.
175.
176.
177.
178.
179.
180.
181.
182.
183.
184.
185.
186.
187.
188.
189.
190.
191.
192.
193.
194.
195.
196.
197.
198.
199.
200.
201.
202.
203.
204.
205.
206.
207.
208.
209.
210.
211.
212.
213.
214.
215.
216.
217.
218.
219.
220.
221.
222.
223.
224.
225.
226.
227.
228.
229.
230.
|
|
<?php
/******************************************************************************/
/* */
/* __ ____ */
/* ___ / / ___ / __/__ __ _____________ ___ */
/* / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-< */
/* / .__/_//_/ .__/___/\___/\_,_/_/ \__/\__/___/ */
/* /_/ /_/ */
/* */
/* */
/******************************************************************************/
/* */
/* Titre : Simple TPL 0.2.3 */
/* */
/* URL : http://www.phpsources.org/scripts518-PHP.htm */
/* Auteur : Supercanard */
/* Date édition : 20 Juin 2009 */
/* Website auteur : http://supercanard.phpnet.org/simpletpl/ */
/* */
/******************************************************************************/
/*
Classe Simple TPL
Version 0.2.3 ( 07/09 )
http://supercanard.phpnet.org/simpletpl/
j.coulet@gmail.com
*/
class simple_tpl{
public $html; // buffer
public $cache_file; // nom du fichier cache
public $dir_cache; // repertoire du cache
public $dir_tpl; // repertoire des templates
protected $temp; // tableau contenu temporaire
protected $incrementation;
// variable contenant les tours de boucle ( utilisee dans prepare_block )
protected $cache_time; // duree du cache
protected $is_in_cache; // page en cache ou non
protected $cache_etat; // on ou off
public function __construct($dir_tpl = './template/', $dir_cache =
'./cache/'){
// construction de l'objet ( @ param repertoires template et cache )
$this->initialise_temp();
$this->define_cache_time(300);
$this->dir_tpl = $dir_tpl;
$this->dir_cache = $dir_cache;
$this->initialise_incrementation();
$this->html = '';
}
public function define_tpl($html_source){
// définie la template a parser puis détermine si il existe dans le cache et
// enfin appelle la méthode d'import des sous templates
$this->html .= file_get_contents($this->dir_tpl.$html_source);
$this->cache_file = md5($html_source).'.html';
// le nom du fichier cache est un hash md5 basé sur son url
$this->is_in_cache = $this->is_in_cache();
$this->import_tpl();
}
public function define_cache_time($second){
// redéfinie la durée du cache et son état
$this->cache_time = time()-$second;
$this->cache_etat = true;
if($second == 0){
$this->cache_etat = false;
}
}
private function is_in_cache(){
// test l'existence d'un fichier cache
if(file_exists($this->dir_cache.$this->cache_file) && filemtime($this->
dir_cache.$this->cache_file) > $this->cache_time){
return true;
}
return false;
}
private function initialise_temp(){
// initialise le tableau $temp
$this->temp = array();
}
private function initialise_incrementation(){
// initialise la variable d'incrementation
$this->incrementation = 0;
}
private function import_tpl(){
// Importe les sous templates dans la template définie par define_tpl()
if(preg_match_all('#{IMPORT (.*)}#Us', $this->html, $matches)){
foreach($matches[1] as $k=>$tpl){
$this->temp['import'] = file_get_contents($this->dir_tpl.$tpl);
$this->html = preg_replace('#'.$matches[0][$k].'#Us', $this->
temp['import'], $this->html);
}
}
}
private function parse_optional(){
// parssage des optional version 0.2.1
$this->html = preg_replace('#{BEGIN OPTIONAL (.*)}#Us', '', $this->html)
;
$this->html = preg_replace('#{END OPTIONAL (.*)}#Us', '', $this->html);
}
private function parse_elseblock($name, $show){
// parssage des elseblock
//echo $show;
if($show == true){
// Affiche elseblock
if(preg_match('#{ELSE '.$name.'}(.*){END ELSE '.$name.'}#Us', $this
->html, $matches)){
$this->html = preg_replace('#'.$matches[0].'#Us', $matches[1],
$this->html);
}
}
if($show == false){
// Retire elseblock
if(preg_match('#{ELSE '.$name.'}(.*){END ELSE '.$name.'}#Us', $this
->html, $matches)){
$this->html = preg_replace('#'.$matches[0].'#Us', '', $this->
html);
}
}
}
public function parse_array($var){
// parssage array ( @param : array variables )
if($this->is_in_cache == false){
foreach($var as $k=>$v){
$recherche[] = '#{'.$k.'}#Us';
$remplace[] = $v;
}
$this->html = preg_replace($recherche, $remplace, $this->html);
}
}
public function prepare_block($nom, $var){
// preparation block ( @param : nom du block, array variables )
if($this->is_in_cache == false){
if($this->incrementation == 0){
// Premier tour de boucle = initialisation + recherche des balises
$this->temp['element'] = '';
$this->temp['first'] = '';
$this->temp['last'] = '';
preg_match('#{BEGIN BLOCK '.$nom.'}(.*){END BLOCK '.$nom.'}#Us',
$this->html, $matches);
$contenu_block = $matches[1];
if(preg_match('#{FIRST}(.*){END FIRST}#Us', $this->html, $matches)){
// recherche si il existe une balise FIRST
// stock first dans $temp
$this->temp['first'] = $matches[1];
// supprime toute la partie FIRST
$contenu_block = preg_replace('#'.$matches[0].'#Us', '',
$contenu_block);
}
if(preg_match('#{LAST}(.*){END LAST}#Us', $this->html, $matches)
){
// recherche si il existe une balise FIRST
// stock last dans $temp
$this->temp['last'] = $matches[1];
// supprime toute la partie FIRST
$contenu_block = preg_replace('#'.$matches[0].'#Us', '',
$contenu_block);
}
// ligne contenant le subject à remplacer à chaque appel de prepare_block dans
// une boucle
$this->temp['ligne'] = $contenu_block;
}
foreach($var as $k=>$v){
$recherche[] = '#{'.$nom.'.'.$k.'}#Us';
$remplace[] = $v;
}
// remplacement du tableau recherche par le tableau des valeurs dans $temp
$this->temp['element'] .= preg_replace($recherche, $remplace, $this
->temp['ligne']);
$this->incrementation++;
}
}
public function parse_block($nom){
// parssage block ( @param : nom du block )
if($this->is_in_cache == false){
if(!isset($this->temp['element'])){
// Si pas d'éléments on remplace par null et on cherche à afficher le elseblock
$this->temp['remplace'] = '';
$this->parse_elseblock($nom, true);
}
else{
$this->temp['remplace'] = $this->temp['first'].$this->temp[
'element'].$this->temp['last'];
$this->parse_elseblock($nom, false);
}
$this->html = preg_replace('#{BEGIN BLOCK '.$nom.'}(.*){END BLOCK '.
$nom.'}#Us', $this->temp['remplace'], $this->html);
// initialisation de $temp et $incrementation pour réutilisation
$this->initialise_temp();
$this->initialise_incrementation();
}
}
public function unset_optional($nom){
// Elimine un optional ( remplace parse_optional() )
if($this->is_in_cache == false){
$this->html = preg_replace('#{BEGIN OPTIONAL '.$nom.
'}(.*){END OPTIONAL '.$nom.'}#Us', '', $this->html);
}
}
public function fetch(){
// Parse les optional, retourne le résultat final et écrit le fichier dans le
// cache
$this->parse_optional();
// si il existe un fichier cache on retourne son contenu
if($this->is_in_cache == true){ return file_get_contents($this->
dir_cache.$this->cache_file); }
// sinon on retourne le résultat du parssage et on le met en cache si
// $cache_etat est true
if($this->cache_etat == true){
file_put_contents($this->dir_cache.$this->cache_file, $this->html);
}
return $this->html;
}
public function display(){
// affichage
echo $this->fetch();
}
}
?>
|