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