Skip to content

Commit 52ec6b8

Browse files
committed
allow to render different components
1 parent 5076b26 commit 52ec6b8

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

flight/template/View.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,31 +172,33 @@ public function fetch(string $file, ?array $data = null): string
172172
$view = ob_get_clean();
173173
}
174174

175-
preg_match(
175+
preg_match_all(
176176
"/<$this->componentPrefix-(?<component>[a-z-]+)\s*(?<props>([a-z]+=\"[a-zA-Z]+\"\s*)*)?\s*\/>/",
177177
$view,
178-
$matches,
178+
$tagsMatches,
179179
);
180180

181-
if ($matches) {
182-
$tag = $matches[0];
183-
$component = $matches['component'];
184-
$props = $matches['props'] ?? '';
181+
$tagsMatches = array_filter($tagsMatches);
182+
183+
foreach ($tagsMatches[0] ?? [] as $tagIndex => $match) {
184+
$tag = $match;
185+
$component = $tagsMatches['component'][$tagIndex];
186+
$props = $tagsMatches['props'][$tagIndex] ?? '';
185187

186188
preg_match_all(
187189
'/(?<name>[a-z]+)="(?<value>[a-zA-Z]+)"/',
188190
$props,
189-
$matches,
191+
$propsMatches,
190192
);
191193

192-
$matches = array_filter($matches);
194+
$propsMatches = array_filter($propsMatches);
193195

194-
if ($matches) {
196+
if ($propsMatches) {
195197
$props = [];
196198

197-
foreach (array_keys($matches[0]) as $index) {
198-
$name = $matches['name'][$index];
199-
$value = $matches['value'][$index];
199+
foreach (array_keys($propsMatches[0]) as $propIndex) {
200+
$name = $propsMatches['name'][$propIndex];
201+
$value = $propsMatches['value'][$propIndex];
200202
$props[$name] = $value;
201203
}
202204
} else {

tests/ViewTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,14 @@ public static function pagesDataProvider(): array
369369
html,
370370
['name' => 'Victoria', 'occupation' => 'Astronaut'],
371371
],
372+
[
373+
'page-with-three-different-components',
374+
<<<'html'
375+
my-component
376+
my-functional-component
377+
my-class-component
378+
html,
379+
],
372380
];
373381
}
374382

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<f-my-component />
2+
<f-my-functional-component />
3+
<f-my-class-component />

0 commit comments

Comments
 (0)