Ik heb momenteel een probleem met mijn php templateParser. Ik denk dat ik weet wat het probleem is, maar ik weet niet hoe ik het moet oplossen. Nu weet ik dat er mensen zijn die zeggen over conditionals en loops in je templates: dat moet je in de code oplossen => je view moet geen conditionals kennen. Deze discussie wil ik laten voor wat het is. Graag wil ik kijken hoe mijn probleem opgelost kan worden. Dus van binnen naar buiten mijn conditionals uitlezen.
Huidige situatie
Ik heb voor mijn templateParser een bestaand conditionals script (CodeIgniter voor PHP) omgebouwd.
Mijn templateParser herkent nu dit soort statements:
Het probleem is wanneer ik het volgende doe:
{if {var1} == {var2}}
asdasdsad
{if {a}=={b}}
asdasd
{/if}
asdddddddddd
{/if}
in dit geval wordt de EERSTE stopif {/if} gematched aan mijn eerste if expressie (De dikgedrukte worden gematched), terwijl hij van binnen naar buiten zou moeten lezen...
Wie weet hoe ik de reguliere expressies zo aanpas dat ik van binnen naar buiten mijn conditionals kan uitlezen?
Huidige situatie
Ik heb voor mijn templateParser een bestaand conditionals script (CodeIgniter voor PHP) omgebouwd.
Mijn templateParser herkent nu dit soort statements:
code:
1
2
3
4
5
6
| {if {var1} == {var2}} blablalbalblabla {/if} of {if {var2}!=""} badfaasd {/if}. |
Het probleem is wanneer ik het volgende doe:
{if {var1} == {var2}}
asdasdsad
{if {a}=={b}}
asdasd
{/if}
asdddddddddd
{/if}
in dit geval wordt de EERSTE stopif {/if} gematched aan mijn eerste if expressie (De dikgedrukte worden gematched), terwijl hij van binnen naar buiten zou moeten lezen...
Wie weet hoe ik de reguliere expressies zo aanpas dat ik van binnen naar buiten mijn conditionals kan uitlezen?
PHP:
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
| /** * Parse conditional statments * Note: This function will ignore no matched or conditional statments with errors * * @access public * @param string * @param bool * @return string */ function conditionals($template, $show_errors) { if (preg_match_all('#'.$this->l_delim.'if (.+)'.$this->r_delim.'(.+)'.$this->l_delim.'/if'.$this->r_delim.'#sU', $template, $conditionals, PREG_SET_ORDER)) { if(count($conditionals) > 0) { foreach($conditionals AS $condition) { $raw_code = (isset($condition[0])) ? $condition[0] : FALSE; $cond_str = (isset($condition[1])) ? $condition[1] : FALSE; $insert = (isset($condition[2])) ? $condition[2] : ''; if(!preg_match('/('.$this->l_delim.'|'.$this->r_delim.')/', $cond_str, $problem_cond)) { // If the the conditional statment is formated right, lets procoess it! if(!empty($raw_code) OR $cond_str != FALSE OR !empty($insert)) { // Get the two values $cond = preg_split("/(\!=|==|<=|>=|<>|<|>|AND|XOR|OR|&&)/", $cond_str); // Do we have a valid if statment? if(count($cond) == 2) { // Get condition preg_match("/(\!=|==|<=|>=|<>|<|>|AND|XOR|OR|&&)/", $cond_str, $cond_m); array_push($cond, $cond_m[0]); // Remove quotes - they cause to many problems! $cond[0] = preg_replace("/[^a-zA-Z0-9s]/", "", $cond[0]); $cond[1] = preg_replace("/[^a-zA-Z0-9s]/", "", $cond[1]); // Test condition eval("\$result = (\"".$cond[0]."\" ".$cond[2]." \"".$cond[1]."\") ? TRUE : FALSE;"); } else { $result = (isset($data[$cond_str])) ? TRUE : FALSE; } } // If the condition is TRUE then show the text block $insert = preg_split('#'.$this->l_delim.'else'.$this->r_delim.'#sU', $insert); if($result == TRUE) { $template = str_replace($raw_code, $insert[0], $template); } else { // Do we have an else statment? if(is_array($insert)) { $insert = (isset($insert[1])) ? $insert[1] : ''; $template = str_replace($raw_code, $insert, $template); } else { $template = str_replace($raw_code, '', $template); } } } elseif(!$show_errors) { // Remove any if statments we can't process $template = str_replace($raw_code, '', $template); } } } } return $template; } |