From 52ec6b88031b1fc0dc97d4d415b17a1237402035 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Thu, 25 Jun 2026 14:43:25 -0400 Subject: [PATCH] allow to render different components --- flight/template/View.php | 26 ++++++++++--------- tests/ViewTest.php | 8 ++++++ .../page-with-three-different-components.php | 3 +++ 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 tests/views/pages/page-with-three-different-components.php diff --git a/flight/template/View.php b/flight/template/View.php index 067d008..e3f8993 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -172,31 +172,33 @@ class View $view = ob_get_clean(); } - preg_match( + preg_match_all( "/<$this->componentPrefix-(?[a-z-]+)\s*(?([a-z]+=\"[a-zA-Z]+\"\s*)*)?\s*\/>/", $view, - $matches, + $tagsMatches, ); - if ($matches) { - $tag = $matches[0]; - $component = $matches['component']; - $props = $matches['props'] ?? ''; + $tagsMatches = array_filter($tagsMatches); + + foreach ($tagsMatches[0] ?? [] as $tagIndex => $match) { + $tag = $match; + $component = $tagsMatches['component'][$tagIndex]; + $props = $tagsMatches['props'][$tagIndex] ?? ''; preg_match_all( '/(?[a-z]+)="(?[a-zA-Z]+)"/', $props, - $matches, + $propsMatches, ); - $matches = array_filter($matches); + $propsMatches = array_filter($propsMatches); - if ($matches) { + if ($propsMatches) { $props = []; - foreach (array_keys($matches[0]) as $index) { - $name = $matches['name'][$index]; - $value = $matches['value'][$index]; + foreach (array_keys($propsMatches[0]) as $propIndex) { + $name = $propsMatches['name'][$propIndex]; + $value = $propsMatches['value'][$propIndex]; $props[$name] = $value; } } else { diff --git a/tests/ViewTest.php b/tests/ViewTest.php index e855fff..34ee4d1 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -369,6 +369,14 @@ class ViewTest extends TestCase html, ['name' => 'Victoria', 'occupation' => 'Astronaut'], ], + [ + 'page-with-three-different-components', + <<<'html' + my-component + my-functional-component + my-class-component + html, + ], ]; } diff --git a/tests/views/pages/page-with-three-different-components.php b/tests/views/pages/page-with-three-different-components.php new file mode 100644 index 0000000..a17fe68 --- /dev/null +++ b/tests/views/pages/page-with-three-different-components.php @@ -0,0 +1,3 @@ + + + \ No newline at end of file