From 8280d4ddaf0291f2273bc8b7d6b2cf4b419ba20c Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Thu, 25 Jun 2026 12:37:56 -0400 Subject: [PATCH] allow to change the prefix --- flight/template/View.php | 11 +++++++--- tests/ViewTest.php | 21 +++++++++++++++++++ ...page-with-component-with-custom-prefix.php | 1 + 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 tests/views/pages/page-with-component-with-custom-prefix.php diff --git a/flight/template/View.php b/flight/template/View.php index d47eeff..1a3dc01 100644 --- a/flight/template/View.php +++ b/flight/template/View.php @@ -27,10 +27,15 @@ class View /** @var array View variables */ protected array $vars = []; + private string $componentPrefix; + /** @param string $path Path to templates directory */ - public function __construct(string $path = '.') - { + public function __construct( + string $path = ".", + string $componentPrefix = 'f' + ) { $this->path = $path; + $this->componentPrefix = $componentPrefix; } /** @@ -158,7 +163,7 @@ class View } preg_match( - '/[a-z-]+)\s*(?([a-z]+="[a-zA-Z]+"\s*)*)?\s*\/>/', + "/<$this->componentPrefix-(?[a-z-]+)\s*(?([a-z]+=\"[a-zA-Z]+\"\s*)*)?\s*\/>/", $view, $matches, ); diff --git a/tests/ViewTest.php b/tests/ViewTest.php index 178c22c..d85b5f6 100644 --- a/tests/ViewTest.php +++ b/tests/ViewTest.php @@ -359,6 +359,27 @@ class ViewTest extends TestCase ]; } + /** @dataProvider prefixesDataProvider */ + public function testRenderComponentsWithDifferentPrefixes(string $prefix): void + { + $view = new View(__DIR__ . '/views', $prefix); + $view->preserveVars = false; + $actual = $view->fetch('pages/page-with-component-with-custom-prefix', compact('prefix')); + $expected = 'my-component'; + + self::assertSame( + self::removeIndentation(self::removeLineEndings($expected)), + self::removeIndentation(self::removeLineEndings($actual)), + ); + } + + public static function prefixesDataProvider(): array + { + return [ + ['x'], + ]; + } + private static function removeLineEndings(string $subject): string { return str_replace(["\r", "\n"], '', $subject); diff --git a/tests/views/pages/page-with-component-with-custom-prefix.php b/tests/views/pages/page-with-component-with-custom-prefix.php new file mode 100644 index 0000000..46b3dce --- /dev/null +++ b/tests/views/pages/page-with-component-with-custom-prefix.php @@ -0,0 +1 @@ +<-my-component /> \ No newline at end of file