6. renders self close components with many props

580-view-class-steroids
fadrian06 1 day ago
parent 38d4458e96
commit 5cdc2e85c0

@ -158,7 +158,7 @@ class View
} }
preg_match( preg_match(
'/<f-(?<component>[a-z-]+)\s*(?<props>[a-z]+="[a-z-A-Z]+")*\s*\/>/', '/<f-(?<component>[a-z-]+)\s*(?<props>([a-z]+="[a-zA-Z]+"\s*)*)?\s*\/>/',
$view, $view,
$matches, $matches,
); );
@ -168,15 +168,25 @@ class View
$component = $matches['component']; $component = $matches['component'];
$props = $matches['props'] ?? ''; $props = $matches['props'] ?? '';
preg_match( preg_match_all(
'/^(?<name>[a-z]+)="(?<value>[a-zA-Z]+)"$/', '/(?<name>[a-z]+)="(?<value>[a-zA-Z]+)"/',
$props, $props,
$matches, $matches,
); );
$props = $matches $matches = array_filter($matches);
? [$matches['name'] => $matches['value']]
: []; if ($matches) {
$props = [];
foreach (array_keys($matches[0]) as $index) {
$name = $matches['name'][$index];
$value = $matches['value'][$index];
$props[$name] = $value;
}
} else {
$props = [];
}
$component = $this->fetch("components/$component", $props); $component = $this->fetch("components/$component", $props);
$view = str_replace($tag, $component, $view); $view = str_replace($tag, $component, $view);

@ -345,7 +345,17 @@ class ViewTest extends TestCase
html, html,
['name' => 'Victoria'], ['name' => 'Victoria'],
], ],
[
'page-with-component-with-two-props',
<<<'html'
<html>
<body>
<h1>Hello, Astronaut Victoria</h1>
</body>
</html>
html,
['name' => 'Victoria', 'occupation' => 'Astronaut'],
],
]; ];
} }

@ -0,0 +1 @@
<h1>Hello, <?= $occupation ?> <?= $name ?></h1>

@ -0,0 +1,5 @@
<html>
<body>
<f-greeting-with-two-props name="<?= $name ?>" occupation="<?= $occupation ?>" />
</body>
</html>
Loading…
Cancel
Save