Aloa,
Ik was vandaag even bezig gegaan met een template parser (proberen althans
).
Dit lukt volgensmij al redelijk. Alleen ben ik nu bij een stuk aangekomen waar ik echt geen idee heb hoe ik het moet aanpakken: De Blocks.
Voor zover ik weet moeten blocks meerdere keren kunnen worden weergegeven. (en ook op dezelfde plek blijven staan zegmaar in de tpl file)
Weet iemand hoe ik dit kan aanpakken?
Voor de geinteresseerde, hier wat ik al heb van mijn template parser:
Ik werk nu dus zegmaar met een var: $blocks[ template_id ] = blockname;
Als iemand nog tips/opmerkingen heeft over deze parser, graag
Ik was vandaag even bezig gegaan met een template parser (proberen althans
Dit lukt volgensmij al redelijk. Alleen ben ik nu bij een stuk aangekomen waar ik echt geen idee heb hoe ik het moet aanpakken: De Blocks.
Voor zover ik weet moeten blocks meerdere keren kunnen worden weergegeven. (en ook op dezelfde plek blijven staan zegmaar in de tpl file)
Weet iemand hoe ik dit kan aanpakken?
Voor de geinteresseerde, hier wat ik al heb van mijn template parser:
Ik werk nu dus zegmaar met een var: $blocks[ template_id ] = blockname;
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
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
| <?php class TemplateParser { /** * The template files dir */ var $dir = null; /** * All initiated templates */ var $loadedTemplates = array(); /** * Content of loaded templates */ var $templateContent = array(); /** * Global assigns */ var $globalAssign = array(); /** * Assigns */ var $assign = array(); /** * Blocks that need to be showed */ var $blocks = array(); /*-------------------------------------------------------------------------*/ // Main constructor /*-------------------------------------------------------------------------*/ function TemplateParser() { $this->dir = root.'template/tpl/'; } /*-------------------------------------------------------------------------*/ // Get the content of a template file /*-------------------------------------------------------------------------*/ function read($files) { $file_array = explode(',', $files); foreach( $file_array as $templateID => $templateName) { $template = $templateName.'.tpl'; if( !file_exists($this->dir.$template) ) { die('The template ('.$template.') does not exists'); } else { if( !$content = file_get_contents($this->dir.$template) ) { die('The template ('.$template.') is unreadable'); } $this->templateContent[] = $content; $this->loadedTemplates[] = $templateName; } } } /*-------------------------------------------------------------------------*/ // Assign global vars /*-------------------------------------------------------------------------*/ function global_assign($before, $after='') { if( is_array($before) ) { foreach( $before as $in => $out ) { $this->globalAssign[$in] = $out; } } else { $this->globalAssign[$before] = $after; } } /*-------------------------------------------------------------------------*/ // Parse global assigns /*-------------------------------------------------------------------------*/ function parse_global_assign() { foreach( $this->loadedTemplates as $templateID => $templateName ) { foreach( $this->globalAssign as $before => $after ) { $this->templateContent[$templateID] = str_replace('{'.$before.'}', $after, $this->templateContent[$templateID]); } } } /*-------------------------------------------------------------------------*/ // Assign a var /*-------------------------------------------------------------------------*/ function assign($before, $after='') { if( is_array($before) ) { foreach( $before as $in => $out ) { $expl = explode('.', $in); $name = $expl[0]; $insert = end($expl); $this->prepare_assign($name, $insert, $out); } } else { $expl = explode('.', $before); $name = $expl[0]; $in = end($expl); $this->prepare_assign($name, $in, $after); } } /*-------------------------------------------------------------------------*/ // Prepare the assign (for more templates with the same name) /*-------------------------------------------------------------------------*/ function prepare_assign($name, $before, $after) { foreach( $this->loadedTemplates as $templateID => $templateName ) { if( $name == $templateName ) { $this->assign[$templateID][$before] = $after; } } } /*-------------------------------------------------------------------------*/ // Parse assigns /*-------------------------------------------------------------------------*/ function parse_assign() { foreach( $this->assign as $templateID => $arr ) { foreach( $this->assign[$templateID] as $before => $after ) { $this->templateContent[$templateID] = str_replace('{'.$before.'}', $after, $this->templateContent[$templateID]); } } } /*-------------------------------------------------------------------------*/ // Show a block /*-------------------------------------------------------------------------*/ function block($blockName) { if( is_array($blockName) ) { foreach( $blockName as $blocks ) { $expl = explode('.', $blocks); $template = $expl[0]; $block = end($expl); $this->prepare_block($template, $block); } } else { $expl = explode('.', $blockName); $template = $expl[0]; $block = end($expl); $this->prepare_block($template, $block); } } /*-------------------------------------------------------------------------*/ // Prepare a block (get the template ID and such) /*-------------------------------------------------------------------------*/ function prepare_block($name, $block) { foreach( $this->loadedTemplates as $templateID => $templateName ) { if( $name == $templateName ) { $this->block[$templateID] = $block; } } } /*-------------------------------------------------------------------------*/ // Parse the blocks /*-------------------------------------------------------------------------*/ function parse_blocks() { // tja, hoe te doen? :') } /*-------------------------------------------------------------------------*/ // Parse the output /*-------------------------------------------------------------------------*/ function parse() { $this->parse_global_assign(); $this->parse_assign(); $this->parse_blocks(); $output = null; foreach( $this->templateContent as $templateID => $content ) { $output .= $content."\n"; } echo $output; } } ?> |
Als iemand nog tips/opmerkingen heeft over deze parser, graag