Skip to content

Commit 70477d9

Browse files
committed
fix: add @loop method
1 parent 473b042 commit 70477d9

1 file changed

Lines changed: 37 additions & 7 deletions

File tree

src/UI/Core.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ public static function render($component)
5757

5858
if ($componentData['responseType'] === 'json') {
5959
unset($componentData['responseType']);
60-
(new \Leaf\Http\Response)->json($componentData);
60+
(new \Leaf\Http\Response())->json($componentData);
6161
} else {
62-
(new \Leaf\Http\Response)->markup($componentData['html']);
62+
(new \Leaf\Http\Response())->markup($componentData['html']);
6363
}
6464
}
6565

@@ -215,15 +215,25 @@ public static function compileTemplate(string $rawText, array $state = []): stri
215215
preg_match('/@if\((.*?)\)/', $compiledWithParsedConditions, $condition);
216216

217217
if (eval("return $condition[1];") === true) {
218-
preg_match('/@if\([\s\S]*?\)\s*[\s\S]*?(?:\s*@elseif\([\s\S]*?\)\s*[\s\S]*?|\s*@else\s*[\s\S]*?|\s*@endif\s*)/', $compiledWithParsedConditions, $ifConditionMatches);
218+
preg_match(
219+
'/@if\([\s\S]*?\)\s*[\s\S]*?(?:\s*@elseif\([\s\S]*?\)\s*[\s\S]*?|\s*@else\s*[\s\S]*?|\s*@endif\s*)/',
220+
$compiledWithParsedConditions,
221+
$ifConditionMatches
222+
);
223+
219224
$renderedData = preg_replace('/@if\([\s\S]*?\)\s*[\s\S]*?/', '', $ifConditionMatches[0]);
220225
$renderedData = preg_replace('/\s*@elseif\([\s\S]*?\)\s*[\s\S]*?/', '', $renderedData);
221226
} else {
222227
if (strpos($compiledWithParsedConditions, '@elseif') !== false) {
223228
preg_match('/@elseif\((.*?)\)/', $compiledWithParsedConditions, $elseifCondition);
224229

225230
if (eval("return $elseifCondition[1];") === true) {
226-
preg_match('/@elseif\([\s\S]*?\)\s*[\s\S]*?(?:\s*@elseif\([\s\S]*?\)\s*[\s\S]*?|\s*@else\s*[\s\S]*?|\s*@endif\s*)/', $compiledWithParsedConditions, $elseifConditionMatches);
231+
preg_match(
232+
'/@elseif\([\s\S]*?\)\s*[\s\S]*?(?:\s*@elseif\([\s\S]*?\)\s*[\s\S]*?|\s*@else\s*[\s\S]*?|\s*@endif\s*)/',
233+
$compiledWithParsedConditions,
234+
$elseifConditionMatches
235+
);
236+
227237
$renderedData = preg_replace('/@elseif\([\s\S]*?\)\s*[\s\S]*?/', '', $elseifConditionMatches[0]);
228238
$renderedData = preg_replace('/\s*@else\s*[\s\S]*?/', '', $renderedData);
229239
} else if (strpos($compiledWithParsedConditions, '@else') !== false) {
@@ -254,7 +264,23 @@ public static function compileTemplate(string $rawText, array $state = []): stri
254264
}, $compiled);
255265

256266
$compiled = preg_replace_callback('/@loop\([\s\S]*?\)\s*[\s\S]*@endloop\s*/', function ($matches) {
257-
return $matches[0];
267+
$rendered = '';
268+
$loopMatches = null;
269+
270+
preg_match('/@loop\((.*?)\)/', $matches[0], $loopMatches);
271+
272+
static::loop(eval("return $loopMatches[1];"), function ($key, $value) use ($matches, &$rendered) {
273+
$regex = '/@loop\((.*?)\)([\s\S]*?)@endloop/';
274+
preg_match($regex, $matches[0], $regexLoopMatches);
275+
276+
$rendered .= str_replace(
277+
['@key', '@value'],
278+
[$key, $value],
279+
$regexLoopMatches[2]
280+
);
281+
});
282+
283+
return $rendered;
258284
}, $compiled);
259285

260286
$compiled = preg_replace_callback('/@case\((.*?)\)/', function ($matches) {
@@ -387,13 +413,17 @@ public static function createStyles(array $styles, array $props = [])
387413
/**
388414
* Loop over an array of items
389415
*
390-
* @param array $array The array to loop through
416+
* @param array|string|int $array The array to loop through
391417
* @param callable $handler Call back function to run per iteration
392418
*/
393-
public static function loop(array $array, callable $handler)
419+
public static function loop($array, callable $handler)
394420
{
395421
$element = "";
396422

423+
if (!is_array($array)) {
424+
$array = explode(',', str_repeat(',', (int) $array - 1));
425+
}
426+
397427
if (is_callable($handler)) {
398428
foreach ($array as $key => $value) {
399429
$element .= call_user_func($handler, $value, $key);

0 commit comments

Comments
 (0)