@@ -103,121 +103,126 @@ public function render(Context $context)
103
103
$ context ->registers ['for ' ] = array ();
104
104
}
105
105
106
- switch ($ this ->type ) {
107
-
108
- case 'collection ' :
109
-
110
- $ collection = $ context ->get ($ this ->collectionName );
111
-
112
- if ($ collection instanceof \Traversable) {
113
- $ collection = iterator_to_array ($ collection );
114
- }
115
-
116
- if (is_null ($ collection ) || !is_array ($ collection ) || count ($ collection ) == 0 ) {
117
- return '' ;
118
- }
119
-
120
- $ range = array (0 , count ($ collection ));
121
-
122
- if (isset ($ this ->attributes ['limit ' ]) || isset ($ this ->attributes ['offset ' ])) {
123
- $ offset = 0 ;
124
-
125
- if (isset ($ this ->attributes ['offset ' ])) {
126
- $ offset = ($ this ->attributes ['offset ' ] == 'continue ' ) ? $ context ->registers ['for ' ][$ this ->name ] : $ context ->get ($ this ->attributes ['offset ' ]);
127
- }
128
-
129
- $ limit = (isset ($ this ->attributes ['limit ' ])) ? $ context ->get ($ this ->attributes ['limit ' ]) : null ;
130
- $ rangeEnd = $ limit ? $ limit : count ($ collection ) - $ offset ;
131
- $ range = array ($ offset , $ rangeEnd );
132
-
133
- $ context ->registers ['for ' ][$ this ->name ] = $ rangeEnd + $ offset ;
134
- }
135
-
136
- $ result = '' ;
137
- $ segment = array_slice ($ collection , $ range [0 ], $ range [1 ]);
138
- if (!count ($ segment )) {
139
- return null ;
140
- }
141
-
142
- $ context ->push ();
143
- $ length = count ($ segment );
144
-
145
- $ index = 0 ;
146
- foreach ($ segment as $ key => $ item ) {
147
- $ value = is_numeric ($ key ) ? $ item : array ($ key , $ item );
148
- $ context ->set ($ this ->variableName , $ value );
149
- $ context ->set ('forloop ' , array (
150
- 'name ' => $ this ->name ,
151
- 'length ' => $ length ,
152
- 'index ' => $ index + 1 ,
153
- 'index0 ' => $ index ,
154
- 'rindex ' => $ length - $ index ,
155
- 'rindex0 ' => $ length - $ index - 1 ,
156
- 'first ' => (int )($ index == 0 ),
157
- 'last ' => (int )($ index == $ length - 1 )
158
- ));
159
-
160
- $ result .= $ this ->renderAll ($ this ->nodelist , $ context );
161
-
162
- $ index ++;
163
-
164
- if (isset ($ context ->registers ['break ' ])) {
165
- unset($ context ->registers ['break ' ]);
166
- break ;
167
- }
168
- if (isset ($ context ->registers ['continue ' ])) {
169
- unset($ context ->registers ['continue ' ]);
170
- }
171
- }
172
-
173
- break ;
174
-
175
- case 'digit ' :
176
-
177
- $ start = $ this ->start ;
178
- if (!is_integer ($ this ->start )) {
179
- $ start = $ context ->get ($ this ->start );
180
- }
181
-
182
- $ end = $ this ->collectionName ;
183
- if (!is_integer ($ this ->collectionName )) {
184
- $ end = $ context ->get ($ this ->collectionName );
185
- }
186
-
187
- $ range = array ($ start , $ end );
188
-
189
- $ context ->push ();
190
- $ result = '' ;
191
- $ index = 0 ;
192
- $ length = $ range [1 ] - $ range [0 ];
193
- for ($ i =$ range [0 ]; $ i <=$ range [1 ]; $ i ++) {
194
- $ context ->set ($ this ->variableName , $ i );
195
- $ context ->set ('forloop ' , array (
196
- 'name ' => $ this ->name ,
197
- 'length ' => $ length ,
198
- 'index ' => $ index + 1 ,
199
- 'index0 ' => $ index ,
200
- 'rindex ' => $ length - $ index ,
201
- 'rindex0 ' => $ length - $ index - 1 ,
202
- 'first ' => (int )($ index == 0 ),
203
- 'last ' => (int )($ index == $ length - 1 )
204
- ));
205
-
206
- $ result .= $ this ->renderAll ($ this ->nodelist , $ context );
207
-
208
- $ index ++;
209
-
210
- if (isset ($ context ->registers ['break ' ])) {
211
- unset($ context ->registers ['break ' ]);
212
- break ;
213
- }
214
- if (isset ($ context ->registers ['continue ' ])) {
215
- unset($ context ->registers ['continue ' ]);
216
- }
217
- }
218
-
219
- break ;
106
+ if ($ this ->type == 'digit ' ) {
107
+ return $ this ->renderDigit ($ context );
108
+ }
109
+
110
+ // that's the default
111
+ return $ this ->renderCollection ($ context );
112
+ }
113
+
114
+ private function renderCollection (Context $ context )
115
+ {
116
+ $ collection = $ context ->get ($ this ->collectionName );
117
+
118
+ if ($ collection instanceof \Traversable) {
119
+ $ collection = iterator_to_array ($ collection );
120
+ }
121
+
122
+ if (is_null ($ collection ) || !is_array ($ collection ) || count ($ collection ) == 0 ) {
123
+ return '' ;
124
+ }
125
+
126
+ $ range = array (0 , count ($ collection ));
127
+
128
+ if (isset ($ this ->attributes ['limit ' ]) || isset ($ this ->attributes ['offset ' ])) {
129
+ $ offset = 0 ;
130
+
131
+ if (isset ($ this ->attributes ['offset ' ])) {
132
+ $ offset = ($ this ->attributes ['offset ' ] == 'continue ' ) ? $ context ->registers ['for ' ][$ this ->name ] : $ context ->get ($ this ->attributes ['offset ' ]);
133
+ }
134
+
135
+ $ limit = (isset ($ this ->attributes ['limit ' ])) ? $ context ->get ($ this ->attributes ['limit ' ]) : null ;
136
+ $ rangeEnd = $ limit ? $ limit : count ($ collection ) - $ offset ;
137
+ $ range = array ($ offset , $ rangeEnd );
138
+
139
+ $ context ->registers ['for ' ][$ this ->name ] = $ rangeEnd + $ offset ;
140
+ }
141
+
142
+ $ result = '' ;
143
+ $ segment = array_slice ($ collection , $ range [0 ], $ range [1 ]);
144
+ if (!count ($ segment )) {
145
+ return null ;
146
+ }
147
+
148
+ $ context ->push ();
149
+ $ length = count ($ segment );
150
+
151
+ $ index = 0 ;
152
+ foreach ($ segment as $ key => $ item ) {
153
+ $ value = is_numeric ($ key ) ? $ item : array ($ key , $ item );
154
+ $ context ->set ($ this ->variableName , $ value );
155
+ $ context ->set ('forloop ' , array (
156
+ 'name ' => $ this ->name ,
157
+ 'length ' => $ length ,
158
+ 'index ' => $ index + 1 ,
159
+ 'index0 ' => $ index ,
160
+ 'rindex ' => $ length - $ index ,
161
+ 'rindex0 ' => $ length - $ index - 1 ,
162
+ 'first ' => (int )($ index == 0 ),
163
+ 'last ' => (int )($ index == $ length - 1 )
164
+ ));
165
+
166
+ $ result .= $ this ->renderAll ($ this ->nodelist , $ context );
167
+
168
+ $ index ++;
169
+
170
+ if (isset ($ context ->registers ['break ' ])) {
171
+ unset($ context ->registers ['break ' ]);
172
+ break ;
173
+ }
174
+ if (isset ($ context ->registers ['continue ' ])) {
175
+ unset($ context ->registers ['continue ' ]);
176
+ }
177
+ }
220
178
179
+ $ context ->pop ();
180
+
181
+ return $ result ;
182
+ }
183
+
184
+ private function renderDigit (Context $ context )
185
+ {
186
+ $ start = $ this ->start ;
187
+ if (!is_integer ($ this ->start )) {
188
+ $ start = $ context ->get ($ this ->start );
189
+ }
190
+
191
+ $ end = $ this ->collectionName ;
192
+ if (!is_integer ($ this ->collectionName )) {
193
+ $ end = $ context ->get ($ this ->collectionName );
194
+ }
195
+
196
+ $ range = array ($ start , $ end );
197
+
198
+ $ context ->push ();
199
+ $ result = '' ;
200
+ $ index = 0 ;
201
+ $ length = $ range [1 ] - $ range [0 ];
202
+ for ($ i = $ range [0 ]; $ i <= $ range [1 ]; $ i ++) {
203
+ $ context ->set ($ this ->variableName , $ i );
204
+ $ context ->set ('forloop ' , array (
205
+ 'name ' => $ this ->name ,
206
+ 'length ' => $ length ,
207
+ 'index ' => $ index + 1 ,
208
+ 'index0 ' => $ index ,
209
+ 'rindex ' => $ length - $ index ,
210
+ 'rindex0 ' => $ length - $ index - 1 ,
211
+ 'first ' => (int )($ index == 0 ),
212
+ 'last ' => (int )($ index == $ length - 1 )
213
+ ));
214
+
215
+ $ result .= $ this ->renderAll ($ this ->nodelist , $ context );
216
+
217
+ $ index ++;
218
+
219
+ if (isset ($ context ->registers ['break ' ])) {
220
+ unset($ context ->registers ['break ' ]);
221
+ break ;
222
+ }
223
+ if (isset ($ context ->registers ['continue ' ])) {
224
+ unset($ context ->registers ['continue ' ]);
225
+ }
221
226
}
222
227
223
228
$ context ->pop ();
0 commit comments