From a94431ae05ca41c3d546a07f64be4c147e236f62 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 24 Jun 2026 00:55:48 -0400 Subject: [PATCH] 1. renders self close components without arguments --- flight/template/View.php | 39 +++++++++++-------- tests/ViewTest.php | 26 +++++++++++++ tests/views/components/my-component.php | 1 + .../page-with-component-with-new-syntax.php | 3 ++ .../page-with-component-with-old-syntax.php | 3 ++ 5 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 tests/views/components/my-component.php create mode 100644 tests/views/pages/page-with-component-with-new-syntax.php create mode 100644 tests/views/pages/page-with-component-with-old-syntax.php diff --git a/flight/template/View.php b/flight/template/View.php index 26c3f2c..c54f7aa 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -92,6 +92,17 @@ class View * @throws Exception If template not found */ public function render(string $file, ?array $templateData = null): void + { + echo $this->fetch($file, $templateData); + } + + /** + * Gets the output of a template + * @param string $file Template file + * @param ?array $data Template data + * @return string Output of template + */ + public function fetch(string $file, ?array $data = null): string { $template = $this->getTemplate($file); @@ -101,30 +112,26 @@ class View extract($this->vars); - if (is_array($templateData)) { - extract($templateData); + if (is_array($data)) { + extract($data); if ($this->preserveVars) { - $this->vars += $templateData; + $this->vars += $data; } } - include $template; - } - - /** - * Gets the output of a template - * @param string $file Template file - * @param ?array $data Template data - * @return string Output of template - */ - public function fetch(string $file, ?array $data = null): string - { ob_start(); + include $template; + $view = ob_get_clean(); + preg_match('/[a-z-]+)\s*\/>/', $view, $matches); - $this->render($file, $data); + if ($matches) { + $tag = $matches[0]; + $component = $this->fetch("components/{$matches['component']}"); + $view = str_replace($tag, $component, $view); + } - return ob_get_clean(); + return $view; } /** diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 91e7b85..d71e166 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -234,6 +234,32 @@ class ViewTest extends TestCase ]; } + /** @dataProvider pagesDataProvider */ + public function testItRendersComponent(string $page, string $expected): void + { + $view = new View(__DIR__ . '/views'); + $view->preserveVars = false; + $actual = $view->fetch($page); + + self::assertSame(self::removeLineEndings($expected), self::removeLineEndings($actual)); + } + + public static function pagesDataProvider(): array + { + return [ + ['pages/page-with-component-with-old-syntax', <<<'html' + + value + + html], + ['pages/page-with-component-with-new-syntax', <<<'html' + + value + + html], + ]; + } + private static function removeLineEndings(string $subject): string { return str_replace(["\r", "\n"], '', $subject); diff --git a/tests/views/components/my-component.php b/tests/views/components/my-component.php new file mode 100644 index 0000000..6d4e150 --- /dev/null +++ b/tests/views/components/my-component.php @@ -0,0 +1 @@ +value diff --git a/tests/views/pages/page-with-component-with-new-syntax.php b/tests/views/pages/page-with-component-with-new-syntax.php new file mode 100644 index 0000000..f02d75a --- /dev/null +++ b/tests/views/pages/page-with-component-with-new-syntax.php @@ -0,0 +1,3 @@ + + + diff --git a/tests/views/pages/page-with-component-with-old-syntax.php b/tests/views/pages/page-with-component-with-old-syntax.php new file mode 100644 index 0000000..ff5cf50 --- /dev/null +++ b/tests/views/pages/page-with-component-with-old-syntax.php @@ -0,0 +1,3 @@ + + render('components/my-component') ?> +