Voilà, j'aimerai faire un générateur de template (en réalité il est déjà fait)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
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
<?php
 
function generate_template($template_path, $array)
{
	if (file_exists($template_path))
		$template = generate_match(file_get_contents($template_path), $array);
	else
		return false;
 
	return $template;
}
 
function generate_match($template, $array)
{
	preg_match_all('/<!-- IF (.*?) -->/U', $template, $match['if']);
	preg_match_all('/<!-- LOOP (.*?) -->/U', $template, $match['loop']);
	preg_match_all('/{(.*)}/U', $template, $match['string']);
 
	$template = generate_template_if($template, $match['if'][1], $array);
	$template = generate_template_loop($template, $match['loop'][1], $array);
	$template = generate_template_string($template, $match['string'][1], $array);
 
	return $template;
}
 
function generate_template_if($template, $match, $array)
{
	for ($i = 0; $i < count($match); $i++)
	{
		if (!empty($array[$match[$i]]))
		{
			preg_match_all('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', $template, $match_if);
			for ($x = 0; $x < count($match_if[1]); $x++)
				$template = preg_replace('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', generate_match($match_if[1][$x], $array), $template);
 
			$template = preg_replace('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', '', $template);
		}
		else
		{
			preg_match_all('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', $template, $match_if);
			for ($x = 0; $x < count($match_if[1]); $x++)
				$template = preg_replace('/<!-- ELSE '.$match[$i].' -->(.*?)<!-- ESLE '.$match[$i].' -->/s', generate_match($match_if[1][$x], $array), $template);
 
			$template = preg_replace('/<!-- IF '.$match[$i].' -->(.*?)<!-- FI '.$match[$i].' -->/s', '', $template);
		}
	}
	return $template;
}
 
function generate_template_loop($template, $match, $array)
{
	for ($i = 0; $i < count($match); $i++)
	{
		if (!empty($array[$match[$i]]))
		{
			preg_match_all('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', $template, $match_loop);
			for ($x = 0; $x < count($match_loop[1]); $x++)
				$template = preg_replace('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', generate_match($match_loop[1][$x], $array[$match[$i]]), $template);
		}
		else
			$template = preg_replace('/<!-- LOOP '.$match[$i].' -->(.*?)<!-- POOL '.$match[$i].' -->/s', '', $template);
	}
	return $template;
}
 
function generate_template_string($template, $match, $array)
{
	for ($i = 0; $i < count($match); $i++)
	{
		if (!empty($array[$match[$i]]))
		{
			if (is_array($array[$match[$i]]))
			{
				for ($x = 0; $x < count($array[$match[$i]]); $x++)
				{
					if (empty($template_array[$x]))
						$template_array[$x] = $template;
 
					$template_array[$x] = str_replace('{'.$match[$i].'}', $array[$match[$i]][$x], $template_array[$x]);
				}
			}
			else
				$template = str_replace('{'.$match[$i].'}', $array[$match[$i]], $template);
		}
		else
			$template = str_replace('{'.$match[$i].'}', '', $template);
	}
	if (!empty($template_array))
	{
		$template = '';
		for ($i = 0; $i < count($template_array); $i++)
			$template .= $template_array[$i];
	}
	return $template;
}
 
?>
Alors le générateur de template gère:
les conditions <!-- IF xxx --> <!-- ELSE xxx -->
les boucles <!-- LOOP xxx -->
les remplacements de string {xxx}

enfin un générateur de template assez basique (assez lourd et a débuguer)

Y'aurait t'il moyen de ne plus utiliser de fonction lourde telque preg_match_all pour la détection des blocks d'instruction du template?


Mais j'aurais aimé savoir si il été vraiment mieux d'utiliser une Class PHP plutôt que plusieurs fonctions?
Si oui, auriez vous un site qui montre comment crée des Class PHP?

Et non, je ne veux pas de système de template tout près comme Smarty ou encore TemplateLite...

je vous remercie d'avance