From 538c9d32ae97d2aad222cdd09b229fecbfa101a6 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Jun 2026 02:44:36 -0400 Subject: [PATCH] 4. renders self close components with one prop --- flight/template/View.php | 21 +++++++++++++++++-- tests/ViewTest.php | 10 +++++++++ tests/views/components/greeting.php | 1 + .../page-with-component-with-one-prop.php | 5 +++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/views/components/greeting.php create mode 100644 tests/views/pages/page-with-component-with-one-prop.php diff --git a/flight/template/View.php b/flight/template/View.php index 06a4416..a7212db 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -157,11 +157,28 @@ class View $view = ob_get_clean(); } - preg_match('/[a-z-]+)\s*\/>/', $view, $matches); + preg_match( + '/[a-z-]+)\s*(?[a-z]+="[a-z-A-Z]+")*\s*\/>/', + $view, + $matches, + ); if ($matches) { $tag = $matches[0]; - $component = $this->fetch("components/{$matches['component']}"); + $component = $matches['component']; + $props = $matches['props'] ?? ''; + + preg_match( + '/^(?[a-z]+)="(?[a-zA-Z]+)"$/', + $props, + $matches, + ); + + $props = $matches + ? [$matches['name'] => $matches['value']] + : []; + + $component = $this->fetch("components/$component", $props); $view = str_replace($tag, $component, $view); } diff --git a/tests/ViewTest.php b/tests/ViewTest.php index a6abff5..3f24028 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -320,6 +320,16 @@ class ViewTest extends TestCase another-class-component extended by my-class-component-that-extends-another-class-component html, ], + [ + 'page-with-component-with-one-prop', + <<<'html' + + +

Hello, James

+ + + html, + ], ]; } diff --git a/tests/views/components/greeting.php b/tests/views/components/greeting.php new file mode 100644 index 0000000..3a8fd49 --- /dev/null +++ b/tests/views/components/greeting.php @@ -0,0 +1 @@ +

Hello,

\ No newline at end of file diff --git a/tests/views/pages/page-with-component-with-one-prop.php b/tests/views/pages/page-with-component-with-one-prop.php new file mode 100644 index 0000000..7244435 --- /dev/null +++ b/tests/views/pages/page-with-component-with-one-prop.php @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file