allow to change the prefix

580-view-class-steroids
fadrian06 2 weeks ago
parent 5cdc2e85c0
commit 8280d4ddaf

@ -27,10 +27,15 @@ class View
/** @var array<string, mixed> 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(
'/<f-(?<component>[a-z-]+)\s*(?<props>([a-z]+="[a-zA-Z]+"\s*)*)?\s*\/>/',
"/<$this->componentPrefix-(?<component>[a-z-]+)\s*(?<props>([a-z]+=\"[a-zA-Z]+\"\s*)*)?\s*\/>/",
$view,
$matches,
);

@ -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);

Loading…
Cancel
Save